Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.6k stars 594 forks source link

In Get Request, I try to encode "+" to "%2b", but it not working. #428

Closed ex-melody closed 2 years ago

ex-melody commented 2 years ago

Describe the bug this is my requeset " https://china-merchant.wish.com/api/v3//products/?sort_by=updated_at.asc&updated_at_min=2022-01-09T16:05:41.041%2b08:00 " i need encode the plus (+) symbol in a URL in a Get request; but this is the error message from wish api "{"message":"'2022-01-09T16:05:41.041%2b08:00' is not a 'date-time' for parameter 'updated_at_min'. Acceptable format should be like YYYY-MM-DDThh:mm:ss.sss+hh:mm","code":1003,"data":{}}"

To Reproduce Steps to reproduce the behavior:

  1. String url = url.replace("+","%2b") String token = getToken(storeAcctId); return Unirest.get(url) .header("authorization", token) .asString();

Expected behavior this same url work in postman,so i wish it work like in postman

Screenshots image

Environmental Data:

ryber commented 2 years ago

Unirestis not postman and we don't have any intent of mimicking it's behavior but as far as I can tell Unirest is doing exactly what you are asking for and sending the param with the encoded +. If I post to http bin I get this:

String body = Unirest.get("https://httpbin.org/get?date=2022-01-09T16:05:41.041%2b08:00") .asString() .getBody();

{
  "args": {
    "date": "2022-01-09T16:05:41.041+08:00"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "unirest-java/3.1.00", 
    "X-Amzn-Trace-Id": "Root=1-6203c7b9-255616fc4bbc7f287fbf86d8"
  }, 
  "origin": "50.81.155.246", 
  "url": "https://httpbin.org/get?date=2022-01-09T16:05:41.041%2b08:00"
}

It looks like from your screen shot that Postman is turning your encoded value back to +, or that might be a artifact of the logger, I don't know, I don't use Postman.

One note is that your datetime isn't really properly encoded, the colons should also be encoded to make a proper URL encoded param. You can do this with formal params like this:

String body = Unirest.get("https://httpbin.org/get") .queryString("date", "2022-01-09T16:05:41.041+08:00") .asString() .getBody();

which results in

{
  "args": {
    "date": "2022-01-09T16:05:41.041+08:00"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "unirest-java/3.1.00", 
    "X-Amzn-Trace-Id": "Root=1-6203c896-44d49c2d2026bf712387fb30"
  }, 
  "origin": "50.81.155.246", 
  "url": "https://httpbin.org/get?date=2022-01-09T16%3A05%3A41.041%2B08%3A00"
}
ex-melody commented 2 years ago

Unirestis not postman and we don't have any intent of mimicking it's behavior but as far as I can tell Unirest is doing exactly what you are asking for and sending the param with the encoded +. If I post to http bin I get this:

String body = Unirest.get("https://httpbin.org/get?date=2022-01-09T16:05:41.041%2b08:00") .asString() .getBody();

{
  "args": {
    "date": "2022-01-09T16:05:41.041+08:00"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "unirest-java/3.1.00", 
    "X-Amzn-Trace-Id": "Root=1-6203c7b9-255616fc4bbc7f287fbf86d8"
  }, 
  "origin": "50.81.155.246", 
  "url": "https://httpbin.org/get?date=2022-01-09T16:05:41.041%2b08:00"
}

It looks like from your screen shot that Postman is turning your encoded value back to +, or that might be a artifact of the logger, I don't know, I don't use Postman.

One note is that your datetime isn't really properly encoded, the colons should also be encoded to make a proper URL encoded param. You can do this with formal params like this:

String body = Unirest.get("https://httpbin.org/get") .queryString("date", "2022-01-09T16:05:41.041+08:00") .asString() .getBody();

which results in

{
  "args": {
    "date": "2022-01-09T16:05:41.041+08:00"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "unirest-java/3.1.00", 
    "X-Amzn-Trace-Id": "Root=1-6203c896-44d49c2d2026bf712387fb30"
  }, 
  "origin": "50.81.155.246", 
  "url": "https://httpbin.org/get?date=2022-01-09T16%3A05%3A41.041%2B08%3A00"
}

image same code,different result { "args": { "date": "2022-01-09T16:05:41.041%2b08:00" }, "headers": { "Accept-Encoding": "gzip", "Host": "httpbin.org", "User-Agent": "unirest-java/1.3.11", "X-Amzn-Trace-Id": "Root=1-620463a8-11c75bf1455b0aa1213bb224" }, "origin": "101.231.195.150", "url": "https://httpbin.org/get?date=2022-01-09T16:05:41.041%252b08:00" }

after update version of Unirest ,it's all fixed