lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Let's say I have a URL ( that I would like
to issue an HTTP POST at with an ASCII string body.  I'm expecting to
get an ASCII string back.  The catch is that I need to set the
content-type of the POST to text/plain.  What's the simplest way to do
this with LuaSocket?

You need to use the general API of the request() function, the
one that takes a table describing the request. The simple
interface assumes application/x-www-form-urlencoded. Here is how
it would look like for your example:

    -- UNTESTED, but should work

    local http = require"socket.http"
    local ltn12 = require"ltn12"

    local reqbody = "this is the body to be sent via post"
    local respbody = {} -- for the response body

    local result, respcode, respheaders, respstatus = http.request {
        method = "POST",
        url = "";,
        source = ltn12.source.string(reqbody),
        headers = {
            ["content-type"] = "text/plain",
            ["content-length"] = tostring(#reqbody)
        sink = ltn12.sink.table(respbody)
    -- get body as string by concatenating table filled by sink
    respbody = table.concat(respbody)

Got it?


PS: Your your example URL shows you know what you are doing. :)