Skip to content
Advertisement

How to solve KeyError: 400 Bad Request

I have a server to which I want to send post requests in the form of json in Java. But after sending, it gives the answer: “KeyError: 400 Bad Request: The browser (or proxy) send a request that this server could not understand.”. No matter how I change the data, it still gives this error, and the sent requests do not appear on the server. How can I fix this?

JavaScript

Advertisement

Answer

This first thing that you need to do is look at the endpoint that you’re POSTing to and determine what it accepts. By this I mean, are you certain that it takes json? Are you certain that your payload is correct?

There’s really no way around it, to consume an API you need to know what it expects.

Secondly, your String str1 does not contain valid JSON, so even if you had all the fields and types correct, this isn’t going to work.

Valid Json would read as

JavaScript

What you have there produces this

JavaScript

Notice the lack of quotations in your string? Even the StackOverflow parser doesn’t know what to make of this 🙂

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement