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

Discussion in 'Android Questions' started by Alejandro Moyano, Jun 17, 2019.

  1. Alejandro Moyano

    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?

    Code:
    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:

    Code:
    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
     

    Attached Files:

  2. Alejandro Moyano

    Alejandro Moyano Member Licensed User

    I solved the problem to make it works i must code:

    Code:
    put.GetRequest.SetContentType("application/json")
    Insetad:

    Code:
    put.GetRequest.SetHeader("Content-Type","application/json")
     
    swamisantosh and DonManfred like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice