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

Wrong response body with PUT request #413

Closed mike818148 closed 3 years ago

mike818148 commented 3 years ago

Describe the bug We are trying to sent PUT request, the response code is 404. But we don't receive the expected error message from the response body.

To Reproduce Steps to reproduce the behavior:

  1. We are running the below request: HttpResponse<String> response = unirestInstance.put($URL).body($BODY).asString();`
  2. Then we call response.getBody() to check the error message

Expected behavior The expected return error message should be: {"ErrorCode":"CAWS00001E","ErrorMessage":"Failed to update *. Reason: **"} Instead, we get the response in pure html as below: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>&#xD;&#xA;&lt;html xmlns="http://www.w3.org/1999/xhtml&quot;>&#xD;&#xA;&lt;head>&#xD;&#xA;&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title>404 - File or directory not found.</title> <style type="text/css"> <!-- body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;} fieldset{padding:0 15px 10px 15px;} h1{font-size:2.4em;margin:0;color:#FFF;} h2{font-size:1.7em;margin:0;color:#CC0000;} h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF; background-color:#555555;} #content{margin:0 0 0 2%;position:relative;} .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;} --> </style> </head> <body> <div id="header"><h1>Server Error</h1></div> <div id="content"> <div class="content-container"><fieldset> <h2>404 - File or directory not found.</h2> <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3> </fieldset></div> </div> </body> </html>

Environmental Data:

Additional context This problem seems only happens to PUT operation, I tried with POST and GET, no such behavior. We tried to use Post-Man to send the same request, the response body display as expected. We also tried to use old com.mashape.unirest.http 1.4.9 and no such behavior, the body message retrieved as expected. (Ps. we just recently upgrade from mashap 1.4.9 to Kong 3.11.13)

ryber commented 3 years ago

that is not an error with Unirest, that is the response you are getting from the remote server

mike818148 commented 3 years ago

Hello @ryber,

Thanks for your quick response. I am just wondering why I can get the same body message with same code while using com.mashape.unirest.http 1.4.9 and also with Post-Man. But while using Kong 3.11.13, the body will just display the 404 html response. But I am pretty sure we having another response body message. Do you have any idea on this?

ryber commented 3 years ago

IDK something is different, you are getting a 404, which is a not found, the body with the HTML is what the server is sending back. Without knowing what is different from the different setups I can't say, but you ARE getting back the response (it's the HTML)

ryber commented 3 years ago

I would check the following:

  1. Is the URL the same
  2. Is the Accepts and Content-Type headers the same ... you aren't setting them in the example, usually servers expect them
  3. Is the body the same
mike818148 commented 3 years ago

Hello @ryber,

Thanks a lot for the hint. I just checked that in our logic we also have the below code to encode the URL:

String val = URLEncoder.encode(val, UTF_8_ENCODING);
The entire URL will be something like: https://abc.com/api/val

I think this is the root cause for the above error. After remove the URLEncoder, it works properly now. May I ask does Kong unirest automatically do the URL encoding for each operations (GET, POST, PUT, DELETE) or we need to do it separately.

Thanks and Regards, Mike

ryber commented 3 years ago

Yes, Unirest handles all the URL encoding. So if you had actual encoded things, then you are going to end up with them being double encoded.

mike818148 commented 3 years ago

Okai. I see. Thanks a lot for your help. This turns to be a logical error in our code. Which leads to double encoding.