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"
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With