-1

I need to get some respones from some URL. For this purpose I use http://unirest.io/java.html and Java.

Map<String, String> map = new HashMap<>();
map.put(key1, value1);
...
map.put(keyN, valueN);

String authToken = "{token}";

HttpResponse<String> response = Unirest.post(url)
    .header("Authorization","Bearer " + authToken)
    .header("Content-Type", "application/json")
    .fields(map)
    .asString();

As a result I receive response.getStatus() = 302 and some unexpected body.

At the same time I use Postman software to get the same responses. The settings are the following:

POST: url
Authorization: Type -> Bearer Token; Token = {{authToken}}  // get the value from the previous request
Header : 
"Authorization" : "Bearer " + {{authToken}}
Content-Type: application/json
Body:
{
    key1 : value1,
    ...
    keyN : valueN
}

And I get some expected response.

What makes the difference?

A user
  • 29
  • 1
  • 9

1 Answers1

4

A 302 is a redirect response. Is it possible Postman is following the redirect and returning the resultant page? Take a look at the Location header in the response you get in Java, and see if following that gives you the same results you're seeing in Postman.

jwismar
  • 12,164
  • 3
  • 32
  • 44
  • This [post](https://support.getpostman.com/hc/en-us/articles/211913929-I-sent-a-POST-request-but-Postman-seems-to-be-sending-a-GET-request-), and other sources I found over the web, confirms that Postman follows every redirection. Fun thing is that it becames [an issue](https://github.com/postmanlabs/postman-app-support/issues/2722) when developpers want to make sure that `302` is properly returned :) – Al-un Oct 29 '18 at 12:49
  • So, do I need to get URL from Location header in the response received in Java, do I need to send POST request with this URL one more time? – A user Oct 29 '18 at 13:14