zig
zig copied to clipboard
std.http: allow fetching with no payload
When a POST, PUT, or PATCH request is sent by Client.fetch without a payload an assert fails in sendBodiless as 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://groups.google.com/g/golang-codereviews/c/VvYtMmdpfAA?pli=1
-
https://github.com/square/okhttp/issues/7005
This 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"
}