Using cURL to upload POST data with files
You need to use the -F
option:-F/--form <name=content> Specify HTTP multipart POST data (H)
Try this:
curl \
-F "userid=1" \
-F "filecomment=This is an image file" \
-F "image=@/home/user1/Desktop/test.jpg" \
localhost/uploader.php
cURL http post file upload using curl --data in linux command line
You usually can't simply pick -F or -d (--data) at your choice. The web server that will receive your post expects one of the formats. If the form you're trying to submit uses the type 'multipart/form-data', then and only then you must use the -F type. If not, you should use -d which then causes a posting with the type 'application/x-www-form-urlencoded'.
multipart/formposts with -F uses a special formatting of the post with Mime-headers separating the different parts and each part having its own set of headers.
-d is just raw data being sent for the server to interpret/decode.
Mentioned in the curl FAQ.
(But since you write the PHP code in this case, you just have to decide which POST method you want to accept and then make your curl command line use that.)
Send request to cURL with post data sourced from a file
You're looking for the --data-binary
argument:
curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "@path/to/file"
In the example above, -i
prints out all the headers so that you can see what's going on, and -X POST
makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an @
symbol, so curl
knows to read from a file.
Correct curl command for uploading local file to url
There's more than one way to "upload a file to a URL", so we cannot actually know unless you give us more details.
But what's clear is that that you lack either a -d or a -F option on your command line, and you should drop the -X POST
.
Multi-part formpost
If you upload with a multipart, which is how most "uploads" to HTTP works, it could be something like this:
curl https://waapi.pepipost.com/api/v2/media/upload/ -H 'Authorization: Bearer myAuthorizationToken' -F "file=@C:\Users\Slomil\Desktop\UserGuide.pdf"
Note that this command line sets the upload part to get the name file, which you should change to the name you want.
"regular" POST
If you just want to send the binary file "raw" in a POST (which your setting of Content-Type might indicate you want), use --data-binary like this:
curl https://waapi.pepipost.com/api/v2/media/upload/ -H 'Authorization: Bearer myAuthorizationToken' -H 'Content-Type: document' --data-binary "@C:\Users\Slomil\Desktop\UserGuide.pdf"
(I copied the Content-Type from the question, although it looks unusual and odd.)
Curl POST upload file test
This is where you are getting it wrong.
file=@demo2_test_imageinput.png
As per your function def, your multipart field name should be upload_file
async def create_upload_file(upload_file: UploadFile = File(...))
So your curl request must have the parameter
upload_file=@demo2_test_imageinput.png
http file upload using cURL in linux(command line)
Try
curl -i -F filename=image.jpg -F image=@/path/to/image.jpg http://localhost/xmlcreate/curlupload.php
posting a file with curl command?
tl;dr
The type
option allows you to specify the Content-Type
of a given part, in a multipart request.
The -F
option (and the more verbose --form
) emulates a filled-in form in which a user has pressed the submit button. It will make curl to POST
data using the Content-Type
header with the multipart/form-data
value.
In multipart requests, each part may have an optional Content-Type
header, as stated in the RFC 7578:
4.4. Content-Type Header Field for Each Part
Each part MAY have an (optional)
Content-Type
header field, which defaults totext/plain
. If the contents of a file are to be sent, the file data SHOULD be labeled with an appropriate media type, if known, orapplication/octet-stream
.
While @
makes a file to get attached in the form as a file upload, the type
parameter allows you to specify the Content-Type
of a given part.
What is the right way to POST multipart/form-data using curl?
The following syntax fixes it for you:
curl -v -F key1=value1 -F upload=@localfilename URL
curl upload all files in a directory with a single curl command?
Use a loop to create an array of alternating -F
and filename=@filename
. Then substitute the array into the curl command.
files=()
while read -r filename; do
files+=(-F "$filename=@$filename")
done < <(find . -name '*.txt' -type f)
curl -k -H "Accept: application/json" -H "Authorization: Bearer $bearertoken" "${$files[@]}" -F "deployment-name=${CIRCLE_SHA1:0:7}" -F "deployment-source=circleci" -F "enable-duplicate-filtering=false" -F "deploy-changed-only=true" "${$files[@]}" http:www.blah.com
Related Topics
Svn Setup of Existing Directory
Creating an Alias in Ubuntu, in .Profile
"Nothing to Commit (Working Directory Clean)" When a Folder Has Been Added
Why Isn't Git Bash Transforming The Path to *Nix Notation for My Python Installation
Difference Between $() and () in Bash
How to Use 9-Bit Serial Communication in Linux
Get The Count of Bytes Waiting on a Serial Port Before Reading, Linux
How to Play an Audio File from Haskell Code, Cross-Platform
Pci-E Memory Space Access with Mmap
Linux Awk Comparing Two CSV Files and Creating a New File with a Flag
Correct Way to Export Multiple Ld_Library_Paths
How to Prevent Remote Branch Deletion in Git Without Using Gitolite
Execute External Program with Trigger in Postgres 9.4
Raspberry-Pi Docker Error: Standard_Init_Linux.Go:178: Exec User Process Caused "Exec Format Error"
On Linux, Is Access() Faster Than Stat()
How to Install G++ on Centos Without Root
Xmonad: Spawnon Workspace That Had Focus When Spawn Key Was Pressed