dongyuan6949 2014-06-17 21:38
浏览 232
已采纳

如何将此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 08: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 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
  • ¥15 一道以太网数据传输题
  • ¥15 python 下载群辉文件
  • ¥50 代码还没怎么运行但是需要代码功能调用数据
  • ¥15 vue请求不到数据,返回状态200,数据为html