Skip to content

Commit 9cc19b3

Browse files
chensunnysriemer
andcommitted
fix: http: send_request: Amend EXPECTING_BODY nil
Update the behavior of the HTTP library to handle cases where the request body is nil for methods that expect a body (e.g., POST, PUT, PATCH). Introduce an error message for such cases and adjust the corresponding tests to align with the new behavior. References: * https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/POST * https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/PUT * https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/PATCH [sparschauer: extended commit description, wrapped a too long line, updated t/15-instance-reuse.t as well] Co-authored-by: Sebastian Parschauer <Sebastian.Parschauer@ibm.com>
1 parent ab7eee3 commit 9cc19b3

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

lib/resty/http.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,8 @@ function _M.send_request(self, params)
706706
headers["Content-Length"] = length
707707

708708
elseif body == nil and EXPECTING_BODY[str_upper(params.method)] then
709-
headers["Content-Length"] = 0
709+
return nil, "Request body is nil but " .. str_upper(params.method)
710+
.. " method expects a body. Use an empty string \"\" if you want to send an empty body."
710711

711712
elseif body ~= nil then
712713
headers["Content-Length"] = #tostring(body)

t/06-simpleinterface.t

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,15 @@ nil closed
269269
lua tcp socket read timed out
270270

271271

272-
=== TEST 7: Content-Length is set on POST/PUT/PATCH requests when body is absent
272+
=== TEST 7: Content-Length is set on POST/PUT/PATCH requests when body is explicitly empty
273273
--- http_config eval: $::HttpConfig
274274
--- config
275275
location = /a {
276276
content_by_lua '
277277
for i, method in ipairs({ "POST", "PUT", "PATCH" }) do
278278
local http = require "resty.http"
279279
local httpc = http.new()
280-
local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method })
280+
local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method, body = "" })
281281
282282
if not res then
283283
ngx.log(ngx.ERR, err)
@@ -336,3 +336,36 @@ Content-Length: nil
336336
[error]
337337
[warn]
338338

339+
340+
=== TEST 9: Error when body is nil for POST/PUT/PATCH requests
341+
--- http_config eval: $::HttpConfig
342+
--- config
343+
location = /a {
344+
content_by_lua '
345+
for i, method in ipairs({ "POST", "PUT", "PATCH" }) do
346+
local http = require "resty.http"
347+
local httpc = http.new()
348+
local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method })
349+
350+
if not res then
351+
ngx.say(method, ": ", err)
352+
else
353+
ngx.say(method, ": success")
354+
end
355+
end
356+
';
357+
}
358+
location = /b {
359+
content_by_lua '
360+
ngx.say("Should not reach here")
361+
';
362+
}
363+
--- request
364+
GET /a
365+
--- response_body
366+
POST: Request body is nil but POST method expects a body. Use an empty string "" if you want to send an empty body.
367+
PUT: Request body is nil but PUT method expects a body. Use an empty string "" if you want to send an empty body.
368+
PATCH: Request body is nil but PATCH method expects a body. Use an empty string "" if you want to send an empty body.
369+
--- no_error_log
370+
[error]
371+
[warn]

t/15-instance-reuse.t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ location /a {
101101
}
102102

103103
local res, err = httpc:request(params)
104-
assert(res, "request should return positvely")
104+
assert(res, "request should return positively")
105105

106106
assert(not params.headers, "params table should not be modified")
107107

108108
local res, err =
109109
httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port, params)
110-
assert(res, "request_uri should return positvely")
110+
assert(res, "request_uri should return positively")
111111

112112
assert(not params.headers, "params table should not be modified")
113113

@@ -116,7 +116,7 @@ location /a {
116116
"connect should return positively")
117117

118118
local pipeline_params = {
119-
{ path = "/b", method = "POST" },
119+
{ path = "/b", method = "POST", body = "" },
120120
{ path = "/b", method = "HEAD" },
121121
}
122122

0 commit comments

Comments
 (0)