Curl request with comma in the directory name in bash - linux

I have problem to execute curl request in the directory has comma in the name using bash command line.
curl --request POST --form "file=#$PWD/input_file" http://HOSTURL.com > output_file
if the directory name is
"test" works
"test test" works
"test, test" doesn't work.
I tried many ways to escape characters like quotations, back slush, changing IFS... but still getting error "failed creating formpost data".
Could someone advise how I should treat such directory names?

This looks like a case curl isn't designed to handle. However, by passing the filename on stdin, you can avoid needing it to correctly parse that value at all.
curl --request POST --form "file=#-" http://HOSTURL.com <input_file >output_file

Related

trying to curl with a file into request and failing [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'

Passing a URL with brackets to curl using bash script

I am trying to get the response from a curl url only for a particular value. For example
i am using the command
URLS=$(curl -g -H "Authorization: ${abc}" "https://api.buildkite.com/v2/organizations/org/agents?meta_data=[queue=dev]")
echo "${URLS}"
The metadata is actually as below:
"meta_data": [
"queue=dev"
]
The above curl command is giving the response for all agents in all queues and not able to get the required ones specific to queue=dev.
What is the correct way to pass url with brackets?

Escape semicolon, double quotes and backslashes for curl

What is the proper way to send this ,./'; '[]}{":?><|\\ as form-data value in curl. I'm doing this
curl --location --request POST 'https://postman-echo.com/post' \
--form 'more=",./'\'';[]}{\":?><|\\\\'"
right now and it gives different result, apparently only 2 backslashes in the response which is supposed to be 4 in total
Response snippet here
Solved!
Apparently this was the problem with my fish shell which was escaping the trailing double quotes. When i ran the same request on bash it was successful.
probably a shell is interpreting your cmd-line and each \\ pair gets reduced to a single \
(revision, even more explicit) :
using echo to show results,
echo more=",./';[]}{\":?><|\\\\\\\\"
adjust if needed, then copy the more="..." part to your curl cmd-line

Invoke Curl from within shell script

I've below code in shell script
#!/bin/bash
oauth_consumer_key='sdfsfsd'
oauth_consumer_key_secret='1sdfsdfs1'
oauth_token='5wrwerwr476a1737fe09de2e4ew'
oauth_token_secret='ec2231779e4'
url='https url goes here'
token=$(./oauth $oauth_consumer_key $oauth_consumer_key_secret $oauth_token $oauth_token_secret GET $url)
curl_path='/usr/bin/curl'
curl_args="-H 'Authorization: $token'"
resp=$($curl_path $curl_args $url)
echo $resp
Here, I'm first running oauth.sh from this program by passing params & capturing output in token which works as expected. And this output looks as shown below:
OAuth oauth_consumer_key="45435", oauth_token="4r43", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1415328827", oauth_nonce="4535345", oauth_version="1.0", oauth_signature="bKeewO%2BTJ7IHjurhtaftn9dNfxA%3D"' 'my url goes here'
Now, I need to invoke curl command from this program by passing above as param as shown below:
curl -H 'Authorization: OAuth oauth_consumer_key="45435", oauth_token="4r43", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1415328827", oauth_nonce="4535345", oauth_version="1.0", oauth_signature="bKeewO%2BTJ7IHjurhtaftn9dNfxA%3D"' 'my url goes here'
When I run above curl command from terminal console, it works but from my shell script it gives error couldn't resolve OAuth
Can anyone help me out in fixing this issue?
Thanks!
It looks like this line is looking for "oauth" in the current directory.
token=$(./oauth
I would try putting the absolute path to oauth instead of ./oauth and see if that works.
If that doesn't work, I would strip it down a little and comment out everything after that line and just echo $token and see if it's showing the expected results and go from there.

How do I POST LF with curl command line tool?

I'm trying to POST to the HTTP gateway of an SMS provider (Sybase 365) using CURL from a Linux shell script.
I need to pass the following data (note the [ ] and LF characters)
[MSISDN]
List=+12345678
[MESSAGE]
Text=Hello
[END]
If I submit a file using the -F parameter, CURL removes the LF e.g.
curl -F #myfile "http://www.sybase.com/..."
results in this at the server (which is rejected)
[MSISDN]List=+12345678[MESSAGE]Text=Hello[END]
Is there anything I can do to avoid this or do I need an alternative tool?
I'm using a file containing my data for testing but I'd like to avoid that in practice and POST directly from the script.
Try using --data-binary instead of -d(ata-ascii).
From the manual:
--data-binary (HTTP) This posts data in a similar manner as --data-ascii does, although when using this option the entire context of the posted data is kept as-is.
If you want to post a binary file without the strip-newlines feature of the --data-ascii option, this is for you. If this option is used several times, the ones following the first will append data.
ETA: oops, I should read the question more closely. You're using -F, not -d. But --data-binary may be still be worth a shot.
Probably a silly thought, but I don't suppose it actually requires CRLF instead of just LF?
Alternatively, have you tried using the --data-binary option instead of -F?
I've got this working using -d
request=`printf "[MSISDN]\nList=$number\n[MESSAGE]\nText=$message\n[END]\n"`
response=`curl -s -u $username:$password -d "$request" http://www.sybase.com/...`
Curiously, if I use -d #myfile (where myfile contains LF separated text), it doesn't work.
I also tried --data-binary without success.
curl "url" --data-binary #myfile
posts new lines in the data [tested on curl 7.12.1]

Resources