0

I'm trying to do a request using a small Java program but I'm getting a 400 - Bad Request as response:

URI uri = new URIBuilder().setScheme("https")
                .setHost("somehost.com")
                .setPath("/API/v1/export").build();
        HttpPost post = new HttpPost(uri);
        post.setHeader("X-API-ID", "myId");
        post.setHeader("Accept", "application/json");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("format", "csv"));
        params.add(new BasicNameValuePair("userId", "userId"));
        post.setEntity(new UrlEncodedFormEntity(params));
        JsonNode responseJson = sendResponseEngineRequest(post);

This responseJson returns the following value:

{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Invalid Content-Type. expected=application/json found=application/x-www-form-urlencoded","errorCode":"RP_0.1"}}}

Thanks in advance.

3 Answers 3

1

The answer is literally in the error you're getting.

You specify you will only accept post.setHeader("Accept", "application/json"); and the error is telling you that what you're requesting is found=application/x-www-form-urlencoded

If you have control over the endpoint you're requesting data, change it to application/json. If you don't change post.setHeader("Accept", "application/json"); to post.setHeader("Accept", "application/x-www-form-urlencoded");

Sign up to request clarification or add additional context in comments.

5 Comments

Could you please tell me how I can request "application/json" and as well how I'm requesting "application/x-www-form-urlencoded" thats not clear for me.
Do you have control of the host you're requesting data from?
Well not really, is a host from a provider.
Then the content type that the URL returns cannot be changed. This means when you request it, you will not be able to get application/json data from it because that's not what it returns. The error message is saying what it's returning 'application/x-www-form-urlencoded'. You could probably clear up your error by changing application/json to application/x-www-form-urlencoded in your code.
Well already tried that, but I'n still getting: {"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Invalid Content-Type. expected=application/json found=application/x-www-form-urlencoded","errorCode":"RP_0.1"}}}
0

Since this is a POST request, you may need to provide both Accept and Content-Type headers.

Accept: What you are expecting to receive. Content-Type: What you are sending to server

post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json");

Comments

0

In my program i am also got this error and found that the link not accepting repeated values.

so please check your link It may not accept any repeated parameters which is already available in that link.

1 Comment

Welcome to SO! As there are additional answers, please explain yours a little bit and the Pros of yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.