How can I use curl to upload a file using paperclip? - ruby-on-rails

I want to upload a file together with some information(e.g. package_type) with curl
in my submission model:
has_attached_file :package
What I tried:
curl -d "submission[package_type]=type1&submission[package]=#/home/ubuntu/Downloads/test.zip" http://localhost:3000/restapi.json
If I leave out the file object, it works(a entry will be inserted into the database)
But I specify the file like above, it gives me an error:
No handler found for "#/home/ubuntu/Downloads/test.zip"
Update:
I just found that that I should use the -F option in curl, but in that case the file information cannot be recorded, is there anyway to include both the file object and file info? Maybe something like curl -d -F ?

I had a similar issue and ended up setting the content-type to multipart/form-data instead of dealing with base64 encoding issues when posting to my REST API. Here is an example which includes headers for auth:
curl -v -H 'Content-Type: multipart/form-data' -H "X-User-Email: <email>" -H "X-User-Token: <token>" -X POST -i -F submission[package_type]=type1 -F submission[image_attributes][image]=#f117.jpg http://localhost:3000/api/v1/submissions

Related

API documentation RAILS

For my first API documentation (very simple API, only one method) I have more or less this structure:
API documentation
1.Disclaimer
2. Using the API
2.1 Input data -> Here I explain how should be the input data, JSON
2.2 Output data > Here I explain which data should be obtained and in JSON
2.3 Example -> I am giving an example of my input and output
In 2.3 I explain that the output (real example in my documentation, I post here only a structure of how it looks) should look like this:
{"message":"Succesful ","data":{"batt1":{"value1":977.48279000017,"value2":977.4208279000022,"value3":1034.9372639500002,"value4":2534.854048049996,"value5":2465.145176450681,"value6":2465.1451764508347},"batt2":{}...}
But its missing me how to put the request in this example.
Until now I have been using/ testing my API with this command: curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '#alldata.json' http://localhost:3000/api/v1/namecontroller
Should I put in my documentaion in 2.3 something like this:
In this example I used the cURL command in the following form: curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '#alldata.json' http://localhost:3000/api/v1/namecontroller
Sorry I am very new to all this, RoR,API etc...
Do you have any idea?
I think you can show it in below format so that will be easy to understand
url: http://localhost:3000/api/v1/namecontroller
method: POST
Body : #alldata.json

Update a page in Confluence using REST API

This is what I've currently got and it creates a new Confluence page. It doesn't update it. Also it posts it in the root space, TST, but I want it to be in TST/space1/subsection2/updateThisPage.
curl -v -u admin:admin -X POST -H Content-Type: application/json -d "{\"id\":\"123456\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"TST\",\"title\":\"updateThisPage\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":3}}" http://localhost:8090/rest/api/content?spaceKey=TST&title=updateThisPage
This is the error message I get
{"statusCode":400,"message":"A page with this title already exists: A page already exists with the title new page in the space with key TST"}
Would it be a permissions error? I know I do not have access to delete.
Use request /rest/api/content/{id}.
This worked for me.
curl -u admin:admin -X PUT -H "Content-Type: application/json" -d "{\"id\":\"26738701\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"RO\"},\"body\":{\"storage\":{\"value\":\"<p>UPDATE This is a new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":2}}" http://localost:10080/rest/api/content/26738701
JSON Payload:
{
"id":"26738701",
"type":"page",
"title":"new page",
"space":{
"key":"RO"
},
"body":{
"storage":{
"value":"<p>UPDATE This is a new page</p>",
"representation":"storage"
}
},
"version":{
"number":2
}
}
Don't forget to use:
content ID in data part
version number in data part
PUT request
content ID in request
Try to use PUT instead of POST.
curl -v -u admin:admin -X PUT -H Content-Type: application/json -d "{\"id\":\"123456\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"TST\",\"title\":\"updateThisPage\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":3}}" http://localhost:8090/rest/api/content?spaceKey=TST&title=updateThisPage
If anyone is looking for javascript solution, here is my answer to another question like that
Unexpected grunt-http error when posting to Atlassian Confluence api
And here you can find working code i've developed on confluence hackathon
https://github.com/devex-web-frontend/dxWebPlugins/blob/master/src/confluence/helpers/buffer.js

How to read file data from CURL param?

In my sinatra application, I am using following curl command to post a file in route /test.
curl -H "Content-Type: text/xml" -vv -X POST -d file_data=#test.xml
http://localhost:4567/test
At my Sinatra post method I want read file like
post '/test' do
data = params[:file_data] #Here file_data param name of CURL command.
end
But, here data is NULL. How should I configure my CURL command to read file from file_data param ?
curl -F file_data=#/some/file/on/your/local/disk http://localhost:4567/test
from cURL manual:
-F, --form <name=content>
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an # sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between # and < is then that # makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.
for more details: cURL manual
Quick answer: Remove -H "Content-Type: text/xml" and it should work.
The problem is that when a file is sent, the Content-Type header should be multipart/form-data, which curl sets automatically when you use -F. From curls documentation:
-F, --form
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit
button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files etc.
However, you are overwriting this header with -H, so your app is expecting something that is not a file.
If, in addition to sending the file with curl, you want to include information about its type, you should instead do this:
curl -X POST -F "file_data=#test.xml;type=text/xml" http://localhost:4567/test
(The content type comes after the file name and a semicolon.)
To see how a raw form submission request ends up looking like with multipart/form-data, check the answers to this question.

How to send file contents as body entity using cURL

I am using cURL command line utility to send HTTP POST to a web service. I want to include a file's contents as the body entity of the POST. I have tried using -d </path/to/filename> as well as other variants with type info like --data </path/to/filename> --data-urlencode </path/to/filename> etc... the file is always attached. I need it as the body entity.
I believe you're looking for the #filename syntax, e.g.:
strip new lines
curl --data "#/path/to/filename" http://...
keep new lines
curl --data-binary "#/path/to/filename" http://...
curl will strip all newlines from the file. If you want to send the file with newlines intact, use --data-binary in place of --data
I know the question has been answered, but in my case I was trying to send the content of a text file to the Slack Webhook api and for some reason the above answer did not work. Anywho, this is what finally did the trick for me:
curl -X POST -H --silent --data-urlencode "payload={\"text\": \"$(cat file.txt | sed "s/\"/'/g")\"}" https://hooks.slack.com/services/XXX
In my case, # caused some sort of encoding problem, I still prefer my old way:
curl -d "$(cat /path/to/file)" https://example.com
curl https://upload.box.com/api/2.0/files/3300/content -H "Authorization: Bearer $access_token" -F file=#"C:\Crystal Reports\Crystal Reports\mysales.pdf"

POST with curl according to a particular format

I'm attempting to gather XML data automatically using curl, and my command so far is
curl -E keyStore.pem -d 'uid=myusername&password=mypassword&active=y&type=F' 'https://www.aidaptest.naimes.faa.gov/aidap/XmlNotamServlet HTTP/1.1/' -k
but it keeps on giving me a "Your browser sent a query this server could not understand." error.
I'm pretty sure that it's connecting since it's not rejecting me, but I don't know how to properly format the POST. Here's some documentation they gave me for the format of the POST request.
POST <URL>/aidap/XmlNotamServlet HTTP/1.1
Content-type: application/x-www-form-urlencoded
Content-length: <input_parameter’s length>
<a blank line>
<input_parameter>`
input_parameter is uid, password, and location_id bit and is correct
Am I doing this correctly from what you can see?
Something like this should do it.
curl -E keyStore.pem -v -X POST -i --header Content-Type:application/x-www-form-urlencoded
-d 'uid=myusername&password=mypassword&active=y&type=F' 'https://www.aidaptest.naimes.faa.gov/aidap/XmlNotamServlet'
I don't think you need HTTP/1.1 at the end of the URL in the command line. And even if you needed it, you certainly don't need the final / character before the closing single quote.

Resources