dongyuan6949 2014-06-18 05:38
浏览 231
已采纳

如何将此curl命令转换为lua?

I have a working .php file on a remote server. If I run the following:

curl -F "USER=user" -F "PASS=pass" -F "TIME=12345" -F "EMAIL=email@email.com" http://myurl.com/create.php

it will return:

{"PASS":"pass","USER":"user"}

However, I'm trying to use the same code with a new interface programmed in lua, and while it DOES contact the server, it also returns "Invalid request". I've setup the .php to return "Invalid request" if the proper POST values aren't set.

I edited the .php to return whatever the input received was, and it's replying:

{"PASS":null,"USER":null}

My .php code is generally as follows:

if (isset($_POST["USER"]) && isset($_POST["PASS"]) && isset($_POST["TIME"]) && isset($_POST["EMAIL"])){
    [...]do stuff[...]
}else{
     sendResponse('Invalid request');
}

And the lua code:

require "socket.http"

function curlCreate()

    response= socket.http.request("http://myurl.com/create.php","USER=user", "PASS=pass", "TIME=12345", "EMAIL=email@email.com")
    print(response)

end

I'm believe that it's perhaps the -F flag provisions mucking up the lua transmission, but documentation on lua curl-ing is a bit sparse and old.

Thanks in advance for any suggestions!

ANSWER DETAILS: The first recommended method of...

response= socket.http.request("http://someurl.com", "USER=user&PASS=pass&TIME=12345&EMAIL=email@email.com")

...did indeed work!

  • 写回答

1条回答 默认 最新

  • donglu8344812 2014-06-18 16:05
    关注

    "Your request call in curlCreate is in the wrong format. The string-argument form of request takes one or two arguments, URL then body. In HTTP, one way of passing all the various parameter settings is to join them with "&", so you could try the second (body) argument to the call as:

    "USER=user&PASS=pass&TIME=12345&EMAIL=email@email.com"
    

    I suspect even that might not work. The curl -F option also sets the content type header (to "multipart/form-data"), and many web apps look for that header as part of validating a request -- I don't know if that's an issue with PHP. To set headers, you need to pass an argument map to request, which is more complicated because the map-argument form is a more general lower-level facility. Something like this:

    local http = require "socket.http"
    local ltn12 = require "ltn12"
    
    function postForm(url, body)
        local sink, responseData = ltn12.sink.table()
        local responseCode, statusCode, headers, statusLine = http.request {
            url = url,
            method = "POST",
            headers = {
                ["Content-Type"] = "application/x-www-form-urlencoded",
                ["Content-Length"] = #body -- this header might not be necessary
            },
            source = ltn12.source.string(body),
            sink = sink        
        }
        return table.concat(responseData), responseCode, -- etc.
    end
    

    (The implementation of the request(url, body) must look much like this.)

    Note that in this example the Content-Type isn't "multipart/form-data": see this question for an explanation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器