Curl filename upload using variable - bash

I would like to store the pathname of a file in a variable for that gets uploaded via cURL. The problem is that I must not have the syntax correct.
curl --user-agent Mozilla -v --form filename=#/path/to/filename -F .submit=Upload http://example.com/upload.cgi
^ the above works just fine, however i would like to store the path in a variable, so when i try:
var=$(pwd)/filename;
curl --user-agent Mozilla -v --form filename=#$var -F .submit=Upload http://example.com/upload.cgi
it comes back as malformed, or the path is missing altogether.

Related

Hiding data/ password while trying curl - bash [duplicate]

I need to make a POST request via cURL from the command line. Data for this request is located in a file. I know that via PUT this could be done with the --upload-file option.
curl host:port/post-file -H "Content-Type: text/xml" --data "contents_of_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.
I need to make a POST request via Curl from the command line. Data for this request is located in a file...
All you need to do is have the --data argument start with a #:
curl -H "Content-Type: text/xml" --data "#path_of_file" host:port/post-file-path
For example, if you have the data in a file called stuff.xml then you would do something like:
curl -H "Content-Type: text/xml" --data "#stuff.xml" host:port/post-file-path
The stuff.xml filename can be replaced with a relative or full path to the file: #../xml/stuff.xml, #/var/tmp/stuff.xml, ...
If you are using form data to upload file,in which a parameter name must be specified , you can use:
curl -X POST -i -F "parametername=#filename" -F "additional_parm=param2" host:port/xxx
Most of answers are perfect here, but when I landed here for my particular problem, I have to upload binary file (XLSX spread sheet) using POST method, I see one thing missing, i.e. usually its not just file you load, you may have more form data elements, like comment to file or tags to file etc as was my case. Hence, I would like to add it here as it was my use case, so that it could help others.
curl -POST -F comment=mycomment -F file_type=XLSX -F file_data=#/your/path/to/file.XLSX http://yourhost.example.com/api/example_url
I was having a similar issue in passing the file as a param. Using -F allowed the file to be passed as form data, but the content type of the file was application/octet-stream. My endpoint was expecting text/csv.
You are able to set the MIME type of the file with the following syntax:
-F 'file=#path/to/file;type=<MIME_TYPE>
So the full cURL command would look like this for a CSV file:
curl -X POST -F 'file=#path/to/file.csv;type=text/csv' https://test.com
There is good documentation on this and other options here: https://catonmat.net/cookbooks/curl/make-post-request#post-form-data
I had to use a HTTP connection, because on HTTPS there is default file size limit.
https://techcommunity.microsoft.com/t5/IIS-Support-Blog/Solution-for-Request-Entity-Too-Large-error/ba-p/501134
curl -i -X 'POST' -F 'file=#/home/testeincremental.xlsx' 'http://example.com/upload.aspx?user=example&password=example123&type=XLSX'

Sending file using CURL in windows

I'm trying to send a file using curl in windows.
Here's the command i'm using:
C:\curl>curl -X POST -F chat_id=#telegramchannel -F photo=#IMAGE.png https://api.telegram.org/bot812312342:XXXXXXXXXXXXXXXXXXXXXX/sendPhoto
and I keep getting this error:
curl: (26) Failed to open/read local data from file/application
does anybody know how to solve it and how to use the -F properly with files on windows?
Thanks
If telegramchannel is not a file, then you have to escape # with a backslash or use single quotes to encapsulate the content. As # has special meaning in curl context,
either
curl -X POST -F chat_id='#telegramchannel' -F photo=#IMAGE.png https://api.telegram.org/bot812312342:XXXXXXXXXXXXXXXXXXXXXX/sendPhoto
or
curl -X POST -F chat_id=\#telegramchannel -F photo=#IMAGE.png https://api.telegram.org/bot812312342:XXXXXXXXXXXXXXXXXXXXXX/sendPhoto

Using variables in CURL

I'm using curl to submit a file unto Skyling and then get the output provided by the site (following this: http://skylign.org/help#api_docs ).
I would want to use a variable file input, and not have the write the file each time directly on the code.
When I write the file (hmmfile.hmm) directly onto the code the output is the one expected. But when I set a variable (HMM) and use the variable as my file then the putput is blank.
This works:
curl -H 'Accept:application/json' -F file='#hmmfile.hmm' -F processing=hmm http://skylign.org
This doesn't:
HMM=$1
curl -H 'Accept:application/json' -F file="${HMM}" -F processing=hmm http://skylign.org
The output should be something like this:
"url":"http://skylign.org:8000/logo/6BBFEB96-E7E0-11E2-A243-DF86A4A34227",
"uuid":"6BBFEB96-E7E0-11E2-A243-DF86A4A34227",
"message":"Logo generated successfully"
You should try…
HMM=$1
curl -H 'Accept:application/json' -F file=#"${HMM}" -F processing=hmm http://skylign.org
# ^
# |
# |
The # is needed as it tells curl the field is a file upload.

Curl changes multipart/form-data path parameter

I try to send some multipart/form-data data using curl in a msys shell to a NAS named Synology. The form-data needs a parameter named "path" and must formated like "/dir/dir2". The slashes can't be changed.
My problem is, when i am using curl the path variable will be changed to "C:/git-sdk-64/dir/dir2" and i don't know how to prevent it. My command looks like this:
curl -X POST \
'http://url:port/webapi/entry.cgi?_sid=secret&api=SYNO.FileStation.Upload&method=upload&version=2' \
-F "path=/dir/dir2" \
-F 'overwrite=true' \
-F 'filename=#/c/Temp/test.txt'
Thanks to Daniel Stenberg's info i found out this is a "problem" with msys self. Msys fills up the path variable. Written down here http://www.mingw.org/wiki/Posix_path_conversion. The solution is to put an semicolon at the end of the path. The complete command now looks like this:
curl -X POST \
'http://url:port/webapi/entry.cgi?_sid=secret&api=SYNO.FileStation.Upload&method=upload&version=2' \
-F "path=/dir/dir2;" \
-F 'overwrite=true' \
-F 'filename=#/c/Temp/test.txt'

How to set command line argument using a variable in bash

I set a command line argument as below:
$TOKENARG='Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33'
curl -v -X DELETE -H $(echo $TOKENARG) http://localhost:3001/api/v1/articles/3
And desired result is:
curl -v -X DELETE -H 'Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33' http://localhost:3001/api/v1/articles/3
But when I run the above one it doesn't work as expected and returns the following messages:
curl: (6) Could not resolve host: Bearer
* Rebuilt URL to: 9042f9a3caacc63419be489aefec02a5eae338c33
Could not resolve host: 9042f9a3caacc63419be489aefec02a5eae338c33
How should I pass argument using a variable?
Because you don't quote your command substitution, the string is split up into three words, Authorization:, Bearer and 9042f9a3caacc63419be489aefec02a5eae338c33. To prevent that, you could quote it:
curl -v -X DELETE -H "$(echo $TOKENARG)" http://localhost:3001/api/v1/articles/3
But you don't need echo and the command substitution at all: $(command) has the same output as command, so you can use
curl -v -X DELETE -H "$TOKENARG" http://localhost:3001/api/v1/articles/3
Side note: all uppercase variable names should be used for environment variables only, see for example the POSIX spec (fourth paragraph), so better would be
$tokenarg='Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33'
curl -v -X DELETE -H "$tokenarg" http://localhost:3001/api/v1/articles/3

Resources