Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua socket POST

I have a hard time using lua socket generic POST. I'm trying to POST to a web along with a body. But the body output i got is 1. Here's my code

local http = require "socket.http"
local ltn12 = require "ltn12"
local util = require "util"
    local reqbody = "anid=&protocol=1&guid=dfe49e55b63f2cf93eb9aabe44b6d9dc5286bbbedfcbf1c75b95f7a4f7439029&d_type=phone&os_version=6.1&ate=1&asid=079ABF64-A23A-4E3B-9000-19A4A608CCBE&affiliate=&modin=7c78d075f379db2f40c9f68df857cb87&os=ios&d_id=107b2734fdb7898251f62d229168484a9d14f7fb654d02d957b30c9f22bb094c&d_code=1E5D02FF-63F3-43A0-A2BF-80E63E00F76C&pn_device_id=&name_hint=iPhone%20Simulator&d_sig=dfe49e55b63f2cf93eb9aabe44b6d9dc5286bbbedfcbf1c75b95f7a4f7439029&hdid=62624a01f8715f2b838224a4a285746d&tracker=&appid=536381662&odin=1da61c680b672c4e114df45cd5f8f0aa9b088338&model=iPhone%20Simulator&ver=15&campaign=&imei=&store_type=apple&"
    local respbody = {} 
    local  body, code, headers, status = http.request {
        method = "POST",
        url = "https://freshdeck.idle-gaming.com/api/guest_session/",
        source = ltn12.source.string(reqbody),
        headers = 
                {
                        ["Accept"] = "*/*",
                        ["Accept-Encoding"] = "gzip, deflate",
                        ["Accept-Language"] = "en-us",
                        ["Content-Type"] = "application/x-www-form-urlencoded",
                        ["content-length"] = string.len(reqbody)
                },
        sink = ltn12.sink.table(respbody)
    }

    LOGINFO('body:' .. tostring(body))
    LOGINFO('code:' .. tostring(code))
    LOGINFO('headers:' .. util.tableToString(headers))
    LOGINFO('status:' .. tostring(status))

and below is the output

body:1
code:200
headers:  "set-cookie": "config_version=887; expires=Sat, 29-Jun-2013 19:07:09 GMT; Max-Age=86400; Path=/"
  "date": "Fri, 28 Jun 2013 19:07:09 GMT"
  "ed-config-version": "887"
  "content-encoding": "gzip"
  "cache-control": "private, no-cache, no-store, must-revalidate"
  "connection": "Close"
  "vary": "Cookie"
  "content-length": "52"
  "pragma": "no-cache"
  "content-type": "application/json; charset=utf-8"
  "server": "nginx/1.2.7"

status:HTTP/1.1 200 OK

I don't know why the body returns 1, any ideas?

Thank you in advance for your help.

like image 521
user1817517 Avatar asked Jun 28 '13 19:06

user1817517


1 Answers

http.request has two forms, The simple form downloads a URL using the GET or POST method and is based on strings.

http.request(url [, body])

The generic form performs any HTTP method and is LTN12 based.

http.request{
  url = string,
  [sink = LTN12 sink,]
  [method = string,]
  [headers = header-table,]
  [source = LTN12 source],
  [step = LTN12 pump step,]
  [proxy = string,]
  [redirect = boolean,]
  [create = function]
}

You are using the generic form, and according to the document, the first return value is supposed to be 1.

In case of failure, the function returns nil followed by an error message. If successful, the simple form returns the response body as a string, followed by the response status code, the response headers and the response status line. The generic function returns the same information, except the first return value is just the number 1 (the body goes to the sink).

like image 98
Yu Hao Avatar answered Nov 16 '22 00:11

Yu Hao