Android Question [SOLVED] When i PUT a JSON the server responses bad request

Alejandro Moyano

Member
Licensed User
Hi im working on a Flask restfull API for a Android App, as is my first android client i having troubles with a PUT request, i send the json but the server responces bad request, the reason: the backend can't parse the json.
I printed it on the log an paste it on "Postman" and its work very well, am i missing something?

B4X:
Private Sub Insert
    Dim put As HttpJob
    Dim root, jsonMap As Map
    Dim parser As JSONParser
    Dim jsonGen As JSONGenerator
    
    jsonMap.Initialize
    jsonMap.Put("id_tipo_de_cliente", id_tipo_de_cliente)
    jsonMap.Put("nombre", nombre)
    jsonMap.Put("direccion",direccion)
    jsonMap.Put("mercado",mercado)
    jsonMap.Put("nave",nave)
    jsonMap.Put("nombre_pila",nombre_pila)
    jsonMap.Put("telefono",telefono)
    jsonGen.Initialize(jsonMap)
    
    put.Initialize("Insert", Me)
    put.PutString($"${REST.API.URL}/clientes"$,jsonGen.ToString)
    put.GetRequest.SetHeader("Authorization",$"Bearer ${REST.API.AccessToken}"$)
    put.GetRequest.SetHeader("Grants",$"${REST.API.Grants.Trim}"$)
    put.GetRequest.SetHeader("Content-Type","application/json")
    Wait For (put) JobDone (put As HttpJob)
    If put.Success Then
        Log(put.GetString)
        parser.Initialize(put.GetString)
        root  = parser.NextObject
        msg = root.Get("message")
        appCode  = root.Get("app_code")
    End If
    put.Release
End Sub

To process in the backend im using a Flask-Restfull with SQLAlchemy:

B4X:
class LIST_ADD_CLIENTES(Resource):   
    @api_access_check("ABMRoles")
    def put(self):
        """Adds a new client"""
        try:
            psr = reqparse.RequestParser()
            psr.add_argument('nombre', required = True, type=str)
            psr.add_argument('direccion', required = False, type=str)
            psr.add_argument('mercado', required = False, type=str)
            psr.add_argument('nave', required = False, type=str)
            psr.add_argument('nombre_pila', required = False, type=str)
            psr.add_argument('telefono', required = False, type=str)
            psr.add_argument('id_tipo_de_cliente', required = True, type=str)
            data = psr.parse_args()
            clientes = CLIENTES(None,data['nombre'],data['direccion'],data['mercado'], \
                data['nave'],data['nombre_pila'],data['telefono'],data['id_tipo_de_cliente'])
            if clientes.save_to_db() == True:
                return {'message': 'the clien was created succesully','app_code' : '0'}, 200
            else:
                return {'message': 'Something went wrong','app_code' : '2'}, 500
        except BadRequest as br:
            return {'message':'The fields: (id_tipo_de_cliente, nombre) are requiered','app_code' : '5'}, 400
        except:
            e = sys.exc_info()
            Log("Excepcion ocurrida en LIST_ADD_CLIENTES put por {0}".format(e))
            return {'message': 'Something went wrong','app_code' : '2'}, 500
 

Attachments

  • Captura.PNG
    Captura.PNG
    47.4 KB · Views: 329
Top