Kong / unirest-java

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

unipart body is wrapped with outer string quotes. #362

Closed haroon-sheikh closed 4 years ago

haroon-sheikh commented 4 years ago

Describe the bug When the request body is a json string, then when getting request body, it is wrapped around additional string quotes.

To Reproduce Steps to reproduce the behavior:

  1. Create the request object
RequestBodyEntity request = Unirest.post("/post")
       .basicAuth("foo", "bar")
       .header("Content-Type", "application/json")
       .queryString("foo", "bar")
       .body("{\"body\": \"sample\"}");

Expected behavior request.getBody().get().uniPart().getValue().toString() should return "{\"body\": \"sample\"}" but returns ""{\"body\": \"sample\"}"" instead.

Screenshots If applicable, add screenshots to help explain your problem. image

Environmental Data:

Additional context Add any other context about the problem here.

ryber commented 4 years ago

I think this is just an artifact of your debugger. This test shows it not to be the case:

https://github.com/Kong/unirest-java/commit/3376fcf817abd3f506d1a1e5fa4b73bf7a1c0e8a

haroon-sheikh commented 4 years ago

Can you try changing to this please:

Object body = "{\"body\": \"sample\"}";
RequestBodyEntity request = Unirest.post(MockServer.POST)
                .basicAuth("foo", "bar")
                .header("Content-Type", "application/json")
                .queryString("foo", "bar")
                .body(body);

I think the issue is when the body type is Object.

ryber commented 4 years ago

Ok, so in that case it's using the objectmapper to serialize the string. So when you pass the object mapper a string, it doesn't know that the object is in fact json, so it formats it as an escaped JSON String ""{\"body\": \"sample\"}"". this is in fact, correct. I suppose we could make the body(Object body) method check if the object is a string and then just pass it through to the string method.

Is there a reason you are declaring the body as just Object when it's a string?

haroon-sheikh commented 4 years ago

The reason I've got Object type is because I have a method for generating request objects with passing in multiple types of body values.

I'm happy to update my implementation if you feel it's not worth fixing this.

ryber commented 4 years ago

Well, "fixing" isn't the right term, you are asking a JSON object mapper to turn a string into a JSON string, and it is doing that. So if we change that, then that is a change in behavior. It might still be the right thing to do, because I'm willing to bet more people use the method by mistake than actually want a string to be a json string. But it's still a breaking change (just a small one)

I'll most likely make the change.

ryber commented 4 years ago

this is complete. It also re-routes Unirest's native JSON types from the body(object) method (JsonNode & JSONElement

done in 3.9.00

haroon-sheikh commented 4 years ago

Thanks for the quick response on this.