Skip to content

JSON object inside object gets stringified incorrectly #196

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
mawkler opened this issue May 26, 2023 · 6 comments
Closed

JSON object inside object gets stringified incorrectly #196

mawkler opened this issue May 26, 2023 · 6 comments

Comments

@mawkler
Copy link

mawkler commented May 26, 2023

Given the following request

POST https://test.com

{
  "foo": {
    "bar": "baz"
  }
}

When I do <Plug>RestNvimPreview, it yields this:

curl -sSL --compressed -X 'POST' --data-raw '{"foo": "{\"bar\": \"baz\"}"}' 'https://test.com'

Notice how the value of foo becomes a string, even though it should be an object.

I expect the follwoing:

curl -sSL --compressed -X 'POST' --data-raw '{"foo": {"bar": "baz"}}' 'https://test.com'
@kejne
Copy link

kejne commented Jun 2, 2023

I got the same issue. When trying to send an array, it would stringify the entire array. Took some time to finally find why it could not unmarshall the json 😂

@thatsmydoing
Copy link

I had to read the code to find this out, but it will only properly serialize the JSON if you have Content-Type: application/json in your headers.

@thatsmydoing
Copy link

thatsmydoing commented Jul 19, 2023

It seems like the rationale for this behavior is in #169 (comment)

Essentially, JSON is special cased to minimize it and non-JSON is intended for form urlencoded but that seems like it's broken?

@mawkler
Copy link
Author

mawkler commented Aug 4, 2023

@thatsmydoing Content-Type: application/json seems to solve the issue like you said, however it feels redundant to have to specify that every time.

One solution would be to use Treesitter to determine the content of the body. When doing :TSPlaygroundToggle on my original example, Treesitter tells me that the body is a json_body.

@boltlessengineer
Copy link
Contributor

Closing this due to v3 release. This should be fixed now.

@mawkler
Copy link
Author

mawkler commented Sep 3, 2024

@boltlessengineer Awesome! Thank you very much!

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

4 participants