0

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!

carlbently
  • 35
  • 6
  • You can simply write `if( isset($_POST["USER"], $_POST["PASS"], $_POST["TIME"], $_POST["EMAIL"]) ) {` – hjpotter92 Jun 18 '14 at 07:14
  • Also, the request is supposed to be: `socket.http.request{ url = "http://myurl.com/create.php?USER=user&PASS=pass&TIME=12345&EMAIL=email@email.com", method = 'POST'}` – hjpotter92 Jun 18 '14 at 07:25

1 Answers1

2

"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.

Community
  • 1
  • 1
Randy Hudson
  • 494
  • 3
  • 4