Skip to content
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

--json with no request items omits body entirely instead of providing empty object #218

Open
lilyball opened this issue Jan 9, 2022 · 3 comments

Comments

@lilyball
Copy link

lilyball commented Jan 9, 2022

If I do a request like xh -p HBhb POST http://example.com --json the outgoing request looks like

POST / HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/json
Host: example.com
User-Agent: xh/0.14.1

Notice the complete lack of any body despite Content-Type: application/json. This is an invalid JSON body.

In the absence of request items, passing --json should pass the body as {}.

xh --version
xh 0.14.1
+native-tls
@blyxxyz
Copy link
Collaborator

blyxxyz commented Jan 9, 2022

This is more or less intentional, it matches HTTPie:

$ http --offline --json post :
POST / HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 0
Content-Type: application/json
Host: localhost
User-Agent: HTTPie/2.6.0

It does seem wrong, so maybe we should change it. I've opened httpie/cli#1263.

(The current best way to send an actual empty JSON object is echo '{}' | xh [...].)

@lilyball
Copy link
Author

Thank you for opening the upstream issue

@ducaale
Copy link
Owner

ducaale commented May 3, 2022

Given that the upstream issue has been closed with no action, I think we should go with @blyxxyz's solution i.e. don't set Content-Type unless there is a JSON body.

httpie/cli#137 already decided opposite by making explicit --json add a Content-Type header, so then maybe that should be reverted instead? Right now it's inconsistent.

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
@lilyball @blyxxyz @ducaale and others