std.http: allow fetching with no payload #26013
Open
+5
−0
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.
When a POST, PUT, or PATCH request is sent by Client.fetch without a payload an assert fails in
sendBodilessas these HTTP methods are expected to have a body. However, the HTTP spec does not require them to have a body.Some prior art on this ambiguity:
https://github.com/square/okhttp/issues/7005This change handles this scenario more gracefully and is consistent with how curl and other "fetch" abstractions do it. It mostly saves some dev tedium from handling this (suppose requests are crafted as user input to a zig executable).
$ curl -X POST http://localhost/echo/post { "args": {}, "data": {}, # <-- "files": {}, "form": {}, "headers": { "host": "localhost", "accept": "*/*", "accept-encoding": "gzip", "user-agent": "curl/8.7.1", "content-length": "0" # <-- }, "json": null, "url": "https://localhost/echo/post" } # null payload vs explicitly empty "" payload $ curl -X POST -d "" http://localhost/echo/post { "args": {}, "data": "", # <-- "files": {}, "form": {}, "headers": { "host": "localhost", "content-length": "0", # <-- "accept-encoding": "gzip", "accept": "*/*", "user-agent": "curl/8.7.1", "content-type": "application/x-www-form-urlencoded" }, "json": null, "url": "localhost/echo/post" }