Jump to content




Lost with HTTP

networking

7 replies to this topic

#1 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 30 May 2014 - 02: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 by SquidDev, 30 May 2014 - 03:32 PM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 30 May 2014 - 03:02 PM

the header as far as I can tell is a key/value pair table

#3 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 30 May 2014 - 03:07 PM

View Posttheoriginalbit, on 30 May 2014 - 03:02 PM, said:

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 by SquidDev, 30 May 2014 - 03:07 PM.


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 30 May 2014 - 03: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.

#5 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 30 May 2014 - 03: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).

#6 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 30 May 2014 - 06:44 PM

Hmm, shouldn't 'postData' be formed just like in GET method?

#postData: key=value&pass=secret


#7 Lignum

  • Members
  • 558 posts

Posted 30 May 2014 - 07:43 PM

Try specifying User-Agent.

#8 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 30 May 2014 - 07:53 PM

View PostMKlegoman357, on 30 May 2014 - 06:44 PM, said:

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.

View PostLignum, on 30 May 2014 - 07:43 PM, said:

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 by SquidDev, 30 May 2014 - 07:53 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users