Start a new topic

Example using curl to post file

Hi folks,



I'm trying to post a file to the Kinvey storage system with curl. This will be useful to us for automated testing and troubleshooting.



I'm following the documentation here: http://devcenter.kinvey.com/rest/guides/files#Uploading



Here's my scripts:



curl -H"Host: baas.kinvey.com" -H"Authorization: Basic xxx" -H"Content-Type: application/json" -d@blob.json -X POST https://baas.kinvey.com/blob/yyy

where xxx is our auth header, base 64 encoded, and yyy is our app name.



blob.json contains:

{

"_filename" : "20090918_042809_TREES_500_1.jpg",

"size" :94522,

"mimeType" :"image/jpeg"

}



It wasn't clear to me what to put in the _acl property, so I skipped that (was that a mistake?)



This returns something like this:

{"_filename":"20090918_042809_TREES_500_1.jpg","size":94522,"mimeType":"image/jpeg","_id":"b827e7c5-6756-47e7-b4d4-2532a8d02045","_acl":{"creator":"yyy"},"_kmd":{"lmt":"2013-12-31T00:23:01.666Z","ect":"2013-12-31T00:23:01.666Z"},"_uploadURL":"http://storage.googleapis.com/c58ccaeeed6c41b6b435663e5cded79b/b827e7c5-6756-47e7-b4d4-2532a8d02045/20090918_042809_TREES_500_1.jpg?GoogleAccessId=558440376632@developer.gserviceaccount.com&Expires=1388449411&Signature=ag3axamwsxgWWWpyUgtQopapQW46X6g%2FmkwSq1AFnDlB5BvzG409mWgNUZw9N882fngdGZO8cWSuW2mVnw00axglEbWDAZoq6oTrC9AosIgu0k6g2dH09ws2pUmgtguEACYD63FrQpTiyo%2B4B5L0pGXvm3K340W5gSUb5gQ1XNI%3D","_expiresAt":"2013-12-31T00:23:31.679Z"}



where yyy is again our app name, and I've changed a few of the values of the query parameters.



I then try to PUT to the _uploadURL:



curl --verbose -H"Content-Length: 94522" -H"Host: storage.googleapis.com" -H"Date: Tue, 31 Dec 2013 00:23:31 GMT" -d@[filename] -X PUT [valued from upload url above]



Based on this url, I have tried both with and without the expect header: http://www.iandennismiller.com/posts/curl-http1-1-100-continue-and-multipartform-data-post.html



I get this output:

* About to connect() to storage.googleapis.com port 80

* Trying 74.125.225.203... connected

* Connected to storage.googleapis.com (74.125.225.203) port 80

> PUT [uploadurl string] HTTP/1.1

> User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5

> Accept: */*

> Content-Length: 94522

> Host: storage.googleapis.com

> Date: Tue, 31 Dec 2013 00:43:31 GMT

> Content-Type: application/x-www-form-urlencoded

> Expect: 100-continue

>




And then it hangs.



I see the files created in the kinvey console, but I get a file not found when I try to download them:



"Firefox can't find the file at https://console.kinvey.com/consoleapi/apps/...."



I have looked at this doc and seem to be following everything: https://developers.google.com/storage/docs/reference-methods#putobject



I also do it within 30 seconds (I think, I'm cutting and pasting pretty quickly) because I know the upload url expires.



Has anyone had success with curl and the REST api for uploading files? Is that _acl field important (seems weird if it is, because the console shows the file metadata, just not the file)?



Any other hints on how to troubleshoot this?



Thanks for your time.

Dan










Hey Dan,

I wanted to let you know we're looking into it. We're a bit light on engineers over the holiday season but we're back in full swing next week. Love your app, by the way!
From your output it looks like you are sending the Expect: 100-continue header. Did you try removing it, as the article suggests (by using -H "Expect:").



If it is hanging then somehow curl does not know how to handle the 100 Continue sent by the server, which is odd. I think I've run into this problem before but can't recall the solution. Try --data-binary instead of -d.

hi i am trying a similar post request to invoke a custom end point. the curl script i have written is like 

curl -H"Host:%host%" -H"Authorization:%auth%" -H"Content-type:application/json" -d '{"ip":"10.10.10.1","id":"sdlkfjsdfi"}' -X POST https://baas.kinvey.com/kid_sdflkjsdf/custom/endpoint


but getting an error saying error:unable to parse json in request. 

What can be done to resolve this issue.

Hi Sundesh,


For your curl request, you need to change "Content-type" header value to "text/plain" to get rid of the error.



Regards,

Wani

Kinvey Support

Thanks wani, it did work.:-)

How do I set _acl and _kmd field from a curl request.?

Hi,


You cannot modify _kmd as it is maintained automatically.


You can pass _acl object in the curl request body ( -d option) directly. This link should help with the body format.



Regards,

Wani

Login or Signup to post a comment