rest.nvim icon indicating copy to clipboard operation
rest.nvim copied to clipboard

JSON object inside object gets stringified incorrectly

Open mawkler opened this issue 2 years ago • 4 comments

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'

mawkler avatar May 26 '23 07:05 mawkler

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 :joy:

kejne avatar Jun 02 '23 09:06 kejne

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 avatar Jul 19 '23 15:07 thatsmydoing

It seems like the rationale for this behavior is in https://github.com/rest-nvim/rest.nvim/pull/169#discussion_r1059405373

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

thatsmydoing avatar Jul 19 '23 15:07 thatsmydoing

@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.

mawkler avatar Aug 04 '23 14:08 mawkler