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 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)