how to do the most basic post ? (how do i add an image to network call ?!)

i think it’s a really basic thing to do , but yet i’m like stuck on it for the past 3 hours…

i’m doing my network request like this > 

function \_M.postImage () local postData = "{\"header\":{},\"body\":{}}" local function commentListener( event ) if ( event.isError ) then print ("error getting file ") else print ( "respons from server: " .. event.response ) end end local params = {} params.body = postData local getScoreURL = "http://noyvehadar.techmarketing.co.il/api/0/drawings?ntv\_plt=1&background\_id=2&text=בוקר טוב&source=http://noyvehadar.techmarketing.co.il/view/images/bubble-right.png" network.request( getScoreURL, "POST", commentListener ) end

and i tried maybe all the possible ways to add a file and yet, i can’t add a file to my post call ?

i just want to add an image tha’ts in my temporary file location

btw, 

if i simulate adding the file with postman (a chrome addon to simulate calls) or with native code there isn’t any problems.

Use network.upload() instead of network.request(), see http://docs.coronalabs.com/api/library/network/upload.html

Also check out this tutorial: http://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

the thing is really when i’m doing a server call i need to add key, i don’t know what it is but whatever it is i need to set it to file… 

again to my understanding with ANY file upload using REST (just normal http calls) you need to set key to somthing so the server will recognize it.

here is a screenshot on how i do it on postman just for you to understand what i’m talking about > 

ng.co.il/api/0/drawings?ntv_plt=1&background_id=2&text=goodmorning"

postman.JPG

PLEASE can anyone help me on how to do it in Lua ?

again the call i’m doing is this > 

http://noyvehadar.techmarketing.co.il/api/0/drawings?ntv_plt=1&background_id=2&text=goodmorning

and i just want to add a file to it and have it as key = file .

…?

Hi @Mars,

Things like this must be encoded for network usage (you can’t just paste them directly as a string). The recent tutorial describes this in greater detail, and should help:

http://coronalabs.com/blog/2014/11/11/tutorial-encoding-urls-for-network-usage/

Take care,

Brent

sorry but still not working, 

it’s still the same problem,

i don’t think the URL is the problem… 

the problem is like i said that i can’t define the key…

i actually gave you the link, and an example in REST client… 

can you make it work at your end ? and then tell me how you did it ?

Here is the problem…  There is no standard way to upload files to servers.  There are many different ways that servers can secure themselves and validate who you are.  While REST is somewhat well defined as far as allowing JSON or XML input/output, using GET, POST, PUT and DELETE to do actions and such, once you get to the actual API call’s its all as the whim of the API provider as to what is expected.

We have no way of knowing what rules your service needs or wants.  Do they have any documentation on how to use this API endpoint?  For instance for the image, you seem to be giving them a URL.  That tells me they want to fetch the image from another webserver.   Some web services want POST requests using multi-part MIME encoding.  Some web services will take straight binary with a PUT request, others will want the binary file base64 encoded before it’s uploaded.  You almost certainly will not ever provide an image as part of the URL if you’re uploading it since GET requests (where the &key=value pairs are part of the URL) are limited to about 256 bytes in length.  POST and PUT are more likely to be the method to upload the data.

For POST requests, you will have to add the data (once you know how it’s to be encoded, i.e. base64)  to the post.body table.  Generally the things in the post.body are also &key=value pairs and have to be specifed as such.  You will need to know the right key’s to use, similar to:

post.body = “someparam=somevalue&filename=” … base64encode(thebinarydataofthefiletoupload)

One of the tutorial’s above covers using PUT. 

Generally authentication is done using the post.header for basic authentication, but again you need to know what’s being asked of you and what key-value pairs they want for that as well.

You need to talk to the provider of the API endpoint you are using (ask them, look for docs on their site, etc.) and find out how they want data sent to them.  Once we know that we might be able to help you format your requests.

Rob

Use network.upload() instead of network.request(), see http://docs.coronalabs.com/api/library/network/upload.html

Also check out this tutorial: http://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

the thing is really when i’m doing a server call i need to add key, i don’t know what it is but whatever it is i need to set it to file… 

again to my understanding with ANY file upload using REST (just normal http calls) you need to set key to somthing so the server will recognize it.

here is a screenshot on how i do it on postman just for you to understand what i’m talking about > 

ng.co.il/api/0/drawings?ntv_plt=1&background_id=2&text=goodmorning"

postman.JPG

PLEASE can anyone help me on how to do it in Lua ?

again the call i’m doing is this > 

http://noyvehadar.techmarketing.co.il/api/0/drawings?ntv_plt=1&background_id=2&text=goodmorning

and i just want to add a file to it and have it as key = file .

…?

Hi @Mars,

Things like this must be encoded for network usage (you can’t just paste them directly as a string). The recent tutorial describes this in greater detail, and should help:

http://coronalabs.com/blog/2014/11/11/tutorial-encoding-urls-for-network-usage/

Take care,

Brent

sorry but still not working, 

it’s still the same problem,

i don’t think the URL is the problem… 

the problem is like i said that i can’t define the key…

i actually gave you the link, and an example in REST client… 

can you make it work at your end ? and then tell me how you did it ?

Here is the problem…  There is no standard way to upload files to servers.  There are many different ways that servers can secure themselves and validate who you are.  While REST is somewhat well defined as far as allowing JSON or XML input/output, using GET, POST, PUT and DELETE to do actions and such, once you get to the actual API call’s its all as the whim of the API provider as to what is expected.

We have no way of knowing what rules your service needs or wants.  Do they have any documentation on how to use this API endpoint?  For instance for the image, you seem to be giving them a URL.  That tells me they want to fetch the image from another webserver.   Some web services want POST requests using multi-part MIME encoding.  Some web services will take straight binary with a PUT request, others will want the binary file base64 encoded before it’s uploaded.  You almost certainly will not ever provide an image as part of the URL if you’re uploading it since GET requests (where the &key=value pairs are part of the URL) are limited to about 256 bytes in length.  POST and PUT are more likely to be the method to upload the data.

For POST requests, you will have to add the data (once you know how it’s to be encoded, i.e. base64)  to the post.body table.  Generally the things in the post.body are also &key=value pairs and have to be specifed as such.  You will need to know the right key’s to use, similar to:

post.body = “someparam=somevalue&filename=” … base64encode(thebinarydataofthefiletoupload)

One of the tutorial’s above covers using PUT. 

Generally authentication is done using the post.header for basic authentication, but again you need to know what’s being asked of you and what key-value pairs they want for that as well.

You need to talk to the provider of the API endpoint you are using (ask them, look for docs on their site, etc.) and find out how they want data sent to them.  Once we know that we might be able to help you format your requests.

Rob