HTTP 400 (bad request) for logical error, not malformed request syntax

Status 422 (RFC 4918, Section 11.2) comes to mind: The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was … Read more

How to send Request payload to REST API in java?

The following code works for me. //escape the double quotes in json string String payload=”{\”jsonrpc\”:\”2.0\”,\”method\”:\”changeDetail\”,\”params\”:[{\”id\”:11376}],\”id\”:2}”; String requestUrl=”https://git.eclipse.org/r/gerrit/rpc/ChangeDetailService”; sendPostRequest(requestUrl, payload); method implementation: public static String sendPostRequest(String requestUrl, String payload) { try { URL url = new URL(requestUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod(“POST”); connection.setRequestProperty(“Accept”, “application/json”); connection.setRequestProperty(“Content-Type”, “application/json; charset=UTF-8”); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), “UTF-8”); … Read more

Getting 400 bad request error in Jquery Ajax POST

Finally, I got the mistake and the reason was I need to stringify the JSON data I was sending. I have to set the content type and datatype in XHR object. So the correct version is here: $.ajax({ type: ‘POST’, url: “http://localhost:8080/project/server/rest/subjects”, data: JSON.stringify({ “subject:title”:”Test Name”, “subject:description”:”Creating test subject to check POST method API”, “sub:tags”: … Read more