krackoder
krackoder

Reputation: 2981

Error 404 with Java HttpClient

I am using Java HttpClient package to make http requests. But I am getting 404. When I try the same request with curl, it works fine. Here's the curl request -

curl -i -X POST http://api/endpoint -H "Content-Type: application/json" -d 'content'

Here's the java code that I am using to implement the above curl request -

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("http://api/endpoint");
post.setHeader("Content-type", "application/json");

post.setEntity(new StringEntity(content));

HttpResponse response = client.execute(post);
logger.info("Response Code : "
            + response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
    result.append(line);
}
logger.info("Response details "+result);

I see error 404 NOT FOUND when I run this java code. What could the problem be?

Upvotes: 3

Views: 3459

Answers (1)

Tanrikut
Tanrikut

Reputation: 548

Remove post.setHeader("Accept", "application/json");

Upvotes: 1

Related Questions