Dissecting cURL into proper network.request code

I’ve been trying to figure out how to make the following call in Corona:

[lua]curl -H “Authorization: <access_token>” -F “encoded_data=@/Users/USER/my_image.jpeg” https://api.website.com[/lua]

Basically I need to upload the .jpeg image but I’m only really comfortable with the -H and -d params currently. Not so much the multipart-form tag and the @ notation. Anyone care to comment?

-H is headers. Typically network.request() takes a table as params. If you look at network.request()'s documentation there is a section on using POST with custom headers:

https://docs.coronalabs.com/api/library/network/request.html#http-post-with-custom-headers

-F is a HTML form submitter. In this case, “encoded_data” is the name of the form field. The text after the = sign is the path to the file. The @ says to actually get the data from the file as binary data.

I think you would actually need to read in the binary data and then run it through a base64 encoder and do something like:

local params = {} params.header = {} params.header.Authorization = "your access token" params.body = {} params.body.encoded\_data = yourBase64encodedBinaryFile network.request("https://api.website.com", "POST", networkListener, params )

or something like that. No guarantee that will work for you because I suspect that cURL is doing some additional processing to make the data Multipart MIME under the hood.

Rob

So why the need to base64 encode?

Hi,

It needs to be base encoded so it can transfer the pretty bits over the network.

Is there any reason you can’t use network.upload?

Cheers.

To be more specific, HTTP is pretty much an ASCII/Text based protocol. On top of that MIME was designed for email which is definitely not binary safe.  Base64 is the most optimal way to convert binary into an ASCII/Text that’s safe to transport over these protocols.

Rob

-H is headers. Typically network.request() takes a table as params. If you look at network.request()'s documentation there is a section on using POST with custom headers:

https://docs.coronalabs.com/api/library/network/request.html#http-post-with-custom-headers

-F is a HTML form submitter. In this case, “encoded_data” is the name of the form field. The text after the = sign is the path to the file. The @ says to actually get the data from the file as binary data.

I think you would actually need to read in the binary data and then run it through a base64 encoder and do something like:

local params = {} params.header = {} params.header.Authorization = "your access token" params.body = {} params.body.encoded\_data = yourBase64encodedBinaryFile network.request("https://api.website.com", "POST", networkListener, params )

or something like that. No guarantee that will work for you because I suspect that cURL is doing some additional processing to make the data Multipart MIME under the hood.

Rob

So why the need to base64 encode?

Hi,

It needs to be base encoded so it can transfer the pretty bits over the network.

Is there any reason you can’t use network.upload?

Cheers.

To be more specific, HTTP is pretty much an ASCII/Text based protocol. On top of that MIME was designed for email which is definitely not binary safe.  Base64 is the most optimal way to convert binary into an ASCII/Text that’s safe to transport over these protocols.

Rob