I have used one POST call inside ngnix sub request to out side resource. May be this can help you to make your own. I m using a request to send a mail via mailgun POST api. These are working examples.
# Mail Endpoints
location ~ ^/api/mail/$ {
# Specify the MIME Type of the Response
default_type 'text/plain';
lua_need_request_body on;
# Access By Lua File
access_by_lua_file /opt/nginx/conf/sites-available/lua/api.mail.lua;
}
# Mail Gun Endpoint internal endpoint does a proxy pass to MailGun
location = /api/mailgun {
internal; # Internal Location Called from sub request only
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://api.mailgun.net/v2/maanas.mailgun.org/messages;
proxy_set_header Authorization "Basic <base64 encoded authentication credentials>";
proxy_read_timeout 60s;
}
--api.mail.lua
if ngx.req.get_method() == "POST" then
-- Read post argument
local args = ngx.req.get_post_args()
local mail = args["mail"]
local from = "email@address"
local subject = "This is a test mail"
local text = "This mail is sent from lua using ngnix sub request."
local message = [[from=]] .. ngx.escape_uri(from) .. [[&to=]] .. ngx.escape_uri(mail) .. [[&subject=]] .. ngx.escape_uri(subject) .. [[&text=]] .. ngx.escape_uri(text)
-- This is where all the magic happens. azentzh has helped to arrive at this.
res = ngx.location.capture('/api/mailgun', {method = ngx.HTTP_POST, body=message})
ngx.say(res.body)
end
What essentially is happening is call is hitting /api/mail it passed the control to access_by_lua_file which inturn executes the subrequest using POST.
@agentzh
Is this async or it work in sync mode only?
I found out there is limitation of no of charactors you can put in access_by_lua but in access_by_lua_file there is no such restrictrions, Is this correct.