Curl changes multipart/form-data path parameter - shell

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'

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 large file as data inside JSON via cURL

I'm trying to send a file as base64-encoded data via POST to the Bugzilla REST API as follows:
curl -X POST https://www.example.com/rest/bug/$id/attachment -H "Content-Type: application/json" \
-d "{\
\"login\" : \"$username\", \
\"password\" : \"$password\", \
\"ids\" : [ $id ], \
\"summary\" : \"...\", \
\"content_type\" : \"application/gzip\", \
\"data\" : \"$data\"\
}"
What I'm getting is an error from cURL that the argument list is too long. Presumably, this is because the file ($data) I'm trying to send is more than the shell maximum (the file is 11M). What I've seen online is that the best way to get around that is to have cURL read the data from a file using --data-binary. But since I need to send a username and password, I'd prefer not to have to save the entire file with them inside.
Is there some way to get around this maximum, or is there another way to send a large amount of data this way? I prefer native Linux tools, as I want this script to be portable.
You can try using a file like this, which is the recommended way.
curl -i \
-H 'Accept:application/json' \
-H 'Authorization:Basic $username:$password' \
-X POST -d #datafile.txt https://www.example.com/rest/bug/$id/attachment

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 call with parameters into variable doesn't work

I'm working with installr API.
I'm trying to do the following curl request via a script :
curl -H "X-InstallrAppToken: mytoken" https://www.installrapp.com/apps.json/ \
-F 'qqfile=#'$APKPATH \
-F 'releaseNotes=These are my release notes' \
-F 'notify=true'
and it works perfectly.
However, when I try to get my release notes from a file with a variable like this :
RELEASENOTES=`cat "release_notes/test.md"`
curl -H "X-InstallrAppToken: mytoken" https://www.installrapp.com/apps.json/ \
-F 'qqfile=#'$APKPATH \
-F 'releaseNotes='$RELEASENOTES \
-F 'notify=true' > /dev/null
it doesn't work at all, only the first word is sent. For the others, I have the error Could not resolve host: xxx.
I did a echo on these two curl request and the exact same thing is printed.
is that the catcommand which return a specific format ?
Probably an issue with the quotes and spaces. You can use double-quotes around a variable to allow variable expansion in the shell.
RELEASENOTES=$(cat "release_notes/test.md")
curl -H "X-InstallrAppToken: mytoken" https://www.installrapp.com/apps.json/ \
-F "qqfile=#${APKPATH}" \
-F "releaseNotes=${RELEASENOTES}" \
-F 'notify=true' > /dev/null

Curl filename upload using variable

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.

Resources