Repository navigation
Send requests without a payload without a body or content type - #78
Merged
Merged
Conversation
NewRequest and NewRequestEx return a payload which is a reader with nothing to read, which was sent as the body, so every GET, HEAD, POST and DELETE without a payload had Content-Type: application/json, which some servers refuse a GET or HEAD for. Send it without a body, so it has no content type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the change to omit bodies and automatic Content-Type headers from payload-free requests.
No open issues 🎉
What changed in this PR
This PR fixes how the client sends its own payload-free requests, omitting the body and automatic Content-Type header so servers do not reject otherwise valid GET or HEAD requests.
Changes:
- Pass a nil body for built-in requests with no buffer, leaving JSON and other payload types unchanged.
- Add regression tests for payload-free GET, HEAD, POST, and DELETE requests, plus JSON payload preservation.
| File | Description |
|---|---|
| do_test.go | Tests absent content type and empty bodies, and verifies JSON requests remain intact. |
| client.go | Uses a nil body for built-in requests without a payload buffer. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NewRequestandNewRequestExreturn a payload that's anio.Readerwith nothing to read.DoWithContextpassed it tohttp.NewRequestWithContextas the request body, and because the body wasn't nil, every request without a payload was sent withContent-Type: application/json. That included GET, HEAD, POST and DELETE. Some servers refuse a GET or HEAD with a content type.DoWithContextnow sends a request without a body when its payload is one of go-client's own requests with no buffer, so it has no content type. JSON payloads, and other payload types such as streaming and multipart, are unchanged.Tests:
TestDoContentTypeindo_test.gochecks that GET, HEAD, POST and DELETE without a payload arrive with noContent-Type, no length and no body, and that a JSON request still sendsapplication/jsonand its body. It fails without the fix.go test ./...passes, apart frompkg/homeassistant, whose tests call a live server (hay.mutablelogic.com) that doesn't resolve from here. They fail the same way on main.