This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SquidDev's profile picture

Lost with HTTP

Started by SquidDev, 30 May 2014 - 01:42 PM
SquidDev #1
Posted 30 May 2014 - 03:42 PM
There have been several pieces of code that I have written recently using the HTTP API that do just not work. (Before you ask, everything is whitelisted).

api-content.dropbox.com
Trying to access this url using:

http.get("https://api-content.dropbox.com/1/files_put/dropbox/")
--or:
http.post("https://api-content.dropbox.com/1/files_put/dropbox/")
or any other variation just does not work. It will just return nil.

multipart/form-data
I saw this and, like a fool, wondered if I could use multipart/form-data to submit forms via Lua:


local FileName = "Test.lua"
local File = fs.open(FileName, "r")
local FileContents = File.readAll()
File.close()

local URL = "http://echo.200please.com/" --"http://httpbin.org/post"

local Boundary = "79abff9df9a846709f00eeccb840e55d" -- Should be random
local Headers = {
  ["Content-Type"] = "multipart/form-data; boundary="..Boundary
}
Boundary = '--'..Boundary
local PostData = Boundary ..'\r\n'
PostData = PostData .. 'Content-Disposition: form-data; name="uploadedfile"; filename="'..FileName..'"\r\n'
PostData = PostData .. "Content-Type: text/plain; charset=utf-8\r\n\r\n" -- You need a lot.
PostData = PostData .. FileContents .. "\r\n" -- Include file
PostData = PostData .. Boundary .. '--\r\n' -- Final boundry
Headers['Content-Length'] = #PostData

local Result = http.post(URL, PostData, Headers)
local Out = Result.readAll()

print(Out)

Here the server does not even respond, I just get stuck on the http.post call, nothing happens. Any ideas what is happening?

My theory is that the server is still waiting for content and so the connection does not close. I can't prove this though.

Edit: I'm using wireshark to monitor my network, it looks as if nothing is even leaving my computer??? Maybe I'm misreading the figures.
Edited on 30 May 2014 - 02:32 PM
theoriginalbit #2
Posted 30 May 2014 - 04:02 PM
the header as far as I can tell is a key/value pair table
SquidDev #3
Posted 30 May 2014 - 04:07 PM
the header as far as I can tell is a key/value pair table

On the wiki: http.post(string url, string postData [, table headers])

I think that is what I am sending: URL is a string, Header is a string, Headers is a table.
Edited on 30 May 2014 - 02:07 PM
theoriginalbit #4
Posted 30 May 2014 - 04:13 PM
All I'm seeing in line 13–16 is Header, which is a string, there's header flags you've got in that string that you don't have in the table which I'm pretty sure Dropbox needs.
SquidDev #5
Posted 30 May 2014 - 04:31 PM
OK. I wasn't clear. Header is not the HTTP header, it is bad variable naming on my part - it is actually the postData variable. I'm not using dropbox at the moment, just an echo service.

From StackOverflow and the RFCs I'm pretty sure this is correct (This RFC is also useful).
MKlegoman357 #6
Posted 30 May 2014 - 07:44 PM
Hmm, shouldn't 'postData' be formed just like in GET method?


#postData: key=value&pass=secret
Lignum #7
Posted 30 May 2014 - 08:43 PM
Try specifying User-Agent.
SquidDev #8
Posted 30 May 2014 - 08:53 PM
Hmm, shouldn't 'postData' be formed just like in GET method?


#postData: key=value&pass=secret
No. I'm using the multipart/form-data method of submitting data. This works differently to the normal way. I've linked to the RFCs in a previous post.

Try specifying User-Agent.

Doesn't work. User agent is by default 'Javasomething', I've tried changing it but it doesn't effect anything else. I don't see why it should, the echo server I'm using shouldn't filter anything.
Edited on 30 May 2014 - 06:53 PM