简体   繁体   中英

the difference between Postman and POST request in Java

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM