Forums

The browser (or proxy) sent a request that this server could not understand.

I had a simple API running before, I just had some tickets and when I came to look the API was down, and when I try to call it with the same code as was working before it gives me the error:

The browser (or proxy) sent a request that this server could not understand.

Also, the same code works perfectly on my local.

Also for more context: The GET operation is working fine, this issue is only on the POST operation.

The POST:

class Contratos(Resource):
def post(self):
    parser = reqparse.RequestParser()  # initialize
    parser.add_argument('cpf', required=True)  # add args
    args = parser.parse_args()  # parse arguments to dictionary
    data = search_contract(args['cpf'])
    if data.empty:
        pass
    else:
        data = data.to_dict('records')
        return {'data': data}, 200

The GET:

class Calendario(Resource):
def get(self):
    data = get_calendar()
    if data.empty:
        pass
    else:
        data = data.to_dict('records')
        return {'data': data}, 200

Do you see any relevant entries in your web app's error log? You could add some logging to establish where exactly is the code failing.

The log on Server log

The log on Access log (get worked)

And Error logs are clean without any new logs.

Server Log: https://photos.app.goo.gl/fkfCuRPhPGcMirEH6 Access Log: (The Get worked fine and post crashed with the response: https://photos.app.goo.gl/EMr2ZsZcuoTFCvFN8

{
"message": "The browser (or proxy) sent a request that this server could not understand."
 }

And Error logs are clean without any new logs.

A 400 error could be caused by a number of things. The message that you posted above covers it, but there are many things that it could mean - perhaps you have csrf turned on on your site and are not providing csrf headers. Perhaps the request is coming is without the required argument that you are trying to parse from it. Perhaps there is no data at all in the request and that is causing the problem. Add some logging to your POST handling code so you can see what is being passed in a how far it is getting, then you can use that information to work out what the problem is.

Ok I added some logs and manage to fix, so for someone who was the same issue and use same code as me, I had to add location='args' on parser.

Old:

parser = reqparse.RequestParser()  # initialize
parser.add_argument('cpf', required=True)  # add args

New:

parser = reqparse.RequestParser()  # initialize
parser.add_argument('cpf', type=str, required=True, location='args')  # add args

Glad to hear that you made it work. Thanks for sharing your solution!