From 291f320d3d14be80e88b5ba2caa2fad722c1d153 Mon Sep 17 00:00:00 2001 From: Ri Shen Chen Date: Thu, 10 Jul 2025 14:51:52 +0800 Subject: [PATCH] fix: EXPECTING_BODY nil --- lib/resty/http.lua | 2 +- t/06-simpleinterface.t | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/resty/http.lua b/lib/resty/http.lua index a85f85af..a1afeeaa 100644 --- a/lib/resty/http.lua +++ b/lib/resty/http.lua @@ -706,7 +706,7 @@ function _M.send_request(self, params) headers["Content-Length"] = length elseif body == nil and EXPECTING_BODY[str_upper(params.method)] then - headers["Content-Length"] = 0 + 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." elseif body ~= nil then headers["Content-Length"] = #tostring(body) diff --git a/t/06-simpleinterface.t b/t/06-simpleinterface.t index c828c2d5..d619be9f 100644 --- a/t/06-simpleinterface.t +++ b/t/06-simpleinterface.t @@ -269,7 +269,7 @@ nil closed lua tcp socket read timed out -=== TEST 7: Content-Length is set on POST/PUT/PATCH requests when body is absent +=== TEST 7: Content-Length is set on POST/PUT/PATCH requests when body is explicitly empty --- http_config eval: $::HttpConfig --- config location = /a { @@ -277,7 +277,7 @@ lua tcp socket read timed out for i, method in ipairs({ "POST", "PUT", "PATCH" }) do local http = require "resty.http" local httpc = http.new() - local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method }) + local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method, body = "" }) if not res then ngx.log(ngx.ERR, err) @@ -336,3 +336,37 @@ Content-Length: nil [error] [warn] + +=== TEST 9: Error when body is nil for POST/PUT/PATCH requests +--- http_config eval: $::HttpConfig +--- config + location = /a { + content_by_lua ' + for i, method in ipairs({ "POST", "PUT", "PATCH" }) do + local http = require "resty.http" + local httpc = http.new() + local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method }) + + if not res then + ngx.say(method, ": ", err) + else + ngx.say(method, ": success") + end + end + '; + } + location = /b { + content_by_lua ' + ngx.say("Should not reach here") + '; + } +--- request +GET /a +--- response_body +POST: Request body is nil but POST method expects a body. Use an empty string "" if you want to send an empty body. +PUT: Request body is nil but PUT method expects a body. Use an empty string "" if you want to send an empty body. +PATCH: Request body is nil but PATCH method expects a body. Use an empty string "" if you want to send an empty body. +--- no_error_log +[error] +[warn] +