Skip to content

Commit 291f320

Browse files
committed
fix: EXPECTING_BODY nil
1 parent 1833103 commit 291f320

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

lib/resty/http.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ 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) .. " method expects a body. Use an empty string \"\" if you want to send an empty body."
710710

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

t/06-simpleinterface.t

Lines changed: 36 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,37 @@ 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]
372+

0 commit comments

Comments
 (0)