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

Clarify that Content-Length is required in the HTTP header #1032

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

CrossVR
Copy link

@CrossVR CrossVR commented Feb 22, 2024

Without it your message will be empty and the message will be replaced by "triggered".

I initially missed this because normally this happens when using a GET request instead of POST, so it took a bit of time to figure out I had forgotten the Content-Length header.

Without it your message will be empty and the message will be replaced by "triggered".

I initially missed this because normally this happens when using a GET request instead of POST, so it took a bit of time to figure out I had forgotten the Content-Length header.
@binwiederhier
Copy link
Owner

You don't really need the Content-Length header at all. Here's an example with nc:

$ echo -e "POST /mytopic1 HTTP/1.1\r\nHost: ntfy.sh\r\nConnection: close\r\n\r\nTest with netcat" | nc -v ntfy.sh 80
Connection to ntfy.sh (159.203.148.75) 80 port [tcp/http] succeeded!
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 07 Mar 2024 15:15:07 GMT
Content-Type: application/json
Content-Length: 120
Connection: close
Access-Control-Allow-Origin: *

{"id":"jM0rj01rYhB0","time":1709824507,"expires":1709867707,"event":"message","topic":"mytopic1","message":"triggered"}

Please note that it also works with out the Connection: close though then it'll expect more requests.

@CrossVR
Copy link
Author

CrossVR commented Mar 8, 2024

Your own example shows that you do need the Content-Length header, look at the response you got from the server:

{
    "id":"jM0rj01rYhB0",
    "time":1709824507,
    "expires":1709867707,
    "event":"message",
    "topic":"mytopic1",
    "message":"triggered"
}

The message you sent was "Test with netcat", yet it was replaced by "triggered" just as expected.

@binwiederhier
Copy link
Owner

Whoa 😮

@binwiederhier binwiederhier reopened this Mar 8, 2024
@binwiederhier
Copy link
Owner

My apologies. That's crazy. I guess I've never sent a raw HTTP request...

I think we'll have to update all examples though not just this one.

@CrossVR
Copy link
Author

CrossVR commented Mar 8, 2024

No worries, this is easy to miss because some utilities will implicitly add a Content-Length header if it's missing. But if you send a truly raw HTTP request then it is necessary to manually add it, so it's useful to add to the docs to avoid confusion.

And indeed Connection: close is also good one to add to ensure you don't have a bunch of unused connection open to your server from applications following the examples in the docs.

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

Successfully merging this pull request may close these issues.

None yet

2 participants