Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL POST --data-binary vs --form

Tags:

I have a simple question regarding to the usage of cURL. Didn't find much during my Google search or Man page to get a clear answer.

In here talks about using either --data vs --form on sending file/attachment. I'm curious to know what are the main difference and under what scenarios you would pick --data-binary VS --form ?

The POST "body" can be sent via either --data (for application/x-www-form-urlencoded) or --form (for multipart/form-data):

-F "foo=bar"                  # 'foo' value is 'bar' -F "foo=<foovalue.txt"        # the specified file is sent as plain text input -F "[email protected]"        # the specified file is sent as an attachment  -d "foo=bar" -d "foo=<foovalue.txt" -d "[email protected]" -d "@entirebody.txt"          # the specified file is used as the POST body  --data-binary "@binarybody.jpg" 
like image 378
xbeta Avatar asked Dec 28 '12 01:12

xbeta


People also ask

How does curl POST binary data?

You probably request this: -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 RFC2388. This enables uploading of binary files etc.

How do I POST a binary file?

To upload a binary file, create a multipart request with a part that contains the JSON request body required by the resource, and a part for each file you want to upload. Each file must have its own part. To upload binary data to multiple fields, add a part for each field.

How does HTTP transfer binary data?

HTTP is perfectly capable of handling binary data: images are sent over HTTP all the time, and they're binary. People upload and download files of arbitrary data types all the time with no problem.


1 Answers

The difference is explained in the HTML 4.01 Specification section on Forms:

application/x-www-form-urlencoded is the default content type.

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

like image 87
Ceasar Bautista Avatar answered Sep 22 '22 06:09

Ceasar Bautista