Closed haroon-sheikh closed 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
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.
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?
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.
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.
this is complete. It also re-routes Unirest's native JSON types from the body(object) method (JsonNode & JSONElement
done in 3.9.00
Thanks for the quick response on this.
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:
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](https://user-images.githubusercontent.com/13623227/88671152-dcdc5b00-d0dd-11ea-8242-c06c05f718e1.png)
Environmental Data:
Additional context Add any other context about the problem here.