0

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 ?

Masudul
  • 21,823
  • 5
  • 43
  • 58
  • You should use `--data-binary @test.xml` - check this out http://curl.haxx.se/docs/manpage.html#--data-binary – Pramod Solanky Sep 29 '14 at 06:14
  • @PamioSolanky, doesn't work. Still I can't read file from file_data param. – Masudul Sep 29 '14 at 06:18
  • If you just want to `POST` the file then you can use this instead `curl -H "Content-Type: text/xml" -i -F name=test -F file_data=@localfile.jpg http://localhost:4567/test` – Pramod Solanky Sep 29 '14 at 07:57

2 Answers2

0

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

0

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.

mper
  • 191
  • 8