Skip to content

unipart body is wrapped with outer string quotes. #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
haroon-sheikh opened this issue Jul 28, 2020 · 7 comments
Closed

unipart body is wrapped with outer string quotes. #362

haroon-sheikh opened this issue Jul 28, 2020 · 7 comments

Comments

@haroon-sheikh
Copy link
Contributor

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:

  • Java Version - 11
  • Version [e.g. 3.8.06]

Additional context
Add any other context about the problem here.

@haroon-sheikh haroon-sheikh changed the title unipart body is wrapped with outer string. unipart body is wrapped with outer string quotes. Jul 28, 2020
ryber added a commit that referenced this issue Jul 28, 2020
@ryber
Copy link
Collaborator

ryber commented Jul 28, 2020

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

3376fcf

@haroon-sheikh
Copy link
Contributor Author

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
Copy link
Collaborator

ryber commented Jul 28, 2020

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
Copy link
Contributor Author

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
Copy link
Collaborator

ryber commented Jul 28, 2020

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 added a commit that referenced this issue Aug 2, 2020
… as a string rather than passing to the object mapper.
ryber added a commit that referenced this issue Aug 2, 2020
@ryber
Copy link
Collaborator

ryber commented Aug 2, 2020

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

done in 3.9.00

@ryber ryber closed this as completed Aug 2, 2020
@haroon-sheikh
Copy link
Contributor Author

Thanks for the quick response on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants