trying to curl with a file into request and failing [duplicate] - linux

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'

Related

Update/Set Default Headers in the Curl

I have a scenario in which I call multiple API requests with the same headers.
Headers would usually have:
Authorization: Bearer ASADFW&YUWFIANDJDDAjd8sa87sad7sa8dhsahdusabhdw
Content-Type: text/html;charset=UTF-8
etc...
I need to set these headers key-value pair as a default so whenever I curl the APIs these get picked.
Is there a way to do this?
I usually just keep a text file open with the Curl command, something like:
curl -X POST -H "Authorization: Bearer ASADFW&YUWFIANDJDDAjd8sa87sad7sa8dhsahdusabhdw"
-H "Content-Type: text/html;charset=UTF-8" -d 'blah'
http://some_url.com:8080
Then, just copy and paste the above into a terminal and make the changes you want. Note that I have formatted the above on multiple lines for reading purposes only. In practice, the entire Curl command should be on a single line.

What is the reason for curl --data-urlencode error on running curl from within a Windows batch script?

I am using the Text Analytics service (find the language) of the Azure portal and a Windows batch script with Curl which is sending JSON data:
The following Windows batch script using curl and the --data option works:
set data="{'documents':[{'id':1,'text':'your are my guest'}]}"
curl -X POST %endpoint% ^
-H "Content-Type: application/json" ^
--data %data%
However, it does not work when I want to use the --data-urlencode option with the same code.
The output error is:
Request body must be present
I have tried many things (double quote, escape, and so on), but nothing works.
Some ideas?
The solution was already described here
The trick is to use a ('Notepad++' UTF-8 without BOM) file, say data.txt, with the --data-binary option :
curl -X POST %endpoint% ^
-H "Content-Type: application/json" ^
--data-binary #data.txt
Inside the file data.txt :
{'documents':[{'id':1,'text':'your are my guest'}]}
But it works with any language (chinese, hindi, etc...), so you don't have to encode anything. For instance, you could have use, in the data.txt file
{'documents':[{'id':2,'text':'大都会区有它自己的当地路边快餐口味'}]}

using api gitlab to download file

EDIT 2:
It's working I have added raw parameter Thanks a lot to Arty-chan.
curl --insecure --request GET --header 'PRIVATE-TOKEN: Y_F8YP3nUnFbzhxkQvgo' https://gitlabxxxxxxxxxxxxxxx/api/v4/projects/98/repository/files/src%2Fmain%2Fscript%2Fdeploiement%2Fsettings%2Frci%2Fsesameweb2.sh/raw?ref=master >> setting.sh
EDIT:
Thanks to Arty-chan.
I cannot download the file but I have long output. How can I download my sesameweb2.sh file please ?
The sh file should contain some export variable and if I check the content it's not the content of my actual file :(
{"file_name":"sesameweb2.sh","file_path":"src/main/script/deploiement/settings/rci/sesameweb2.sh","size":792,"encoding":"base64","content_sha256":"0ef870c1ebf9e0be5a5a976a4d3e5424a0b5752a75c67fe4b7744eb8fd6279d3","ref":"master","blob_id":"e3937fb8a25edd7a73bbbe5d6b2e267a4e404ef5","commit_id":"577c6c9ba661eb2726b5cd2095bc93bbd7eb3302","last_commit_id":"6921e0acd61d24066a3d680a5d77bd68f880e4d1","content":"IyEvdXNyL2Jpbi9lbnYgYmFzaApleHBvcnQgQU5UX0hPTUU9L3Vzci9hcGFjaGUvYXBhY2hlLWFudC0xLjkuNwpleHBvcnQgTUFWRU5fSE9NRT0vdXNyL2FwYWNoZS9hcGFjaGUtbWF2ZW4tMy4xLjEKZXhwb3J0IEpBVkFfSE9NRT0vdXNyL2phdmEvamRrMS43LjBfNzkKZXhwb3J0IFRPTUNBVF9JTlNUPWFwYWNoZS10b21jYXQtMzEKZXhwb3J0IE5PTV9XQVI9Y3J5c2FsaWQKZXhwb3J0IE5PTV9XQVJfMj1jcnlzYWxpZHdlYgpleHBvcnQgQ09NUFVURVJOQU1FPVMwMFNMUjIwMQpleHBvcnQgUkVGRVJFTlRJRUxfTkFNRT1zZXNhbWV3ZWIKZXhwb3J0IGdyb3VwaWQ9cGFjaWZpY2EuYXBwbGljYXRpb25zLiR7UkVGRVJFTlRJRUxfTkFNRX12MTIKZXhwb3J0IGFydGVmYWN0aWQ9JHtOT01fV0FSfQpleHBvcnQgYXJ0ZWZhY3RpZF8yPSR7Tk9NX1dBUl8yfQpleHBvcnQgYXBwX3ZlcnNpb249TEFURVNUCmV4cG9ydCBuZXh1c191cmw9aHR0cDovL3JlcG9zaXRvcnktbWFuYWdlci5wYWNpZmljYS5jcmVkaXQtYWdyaWNvbGUuZnIvbmV4dXMKZXhwb3J0IG5leHVzX3JlcG89cGFjaWZpY2Etc25hcHNob3RzLXJlY2V0dGUtaW5mby1sb2NhbApleHBvcnQgZW52aXJvbm5lbWVudD1yZWNldHRlSW5mb3JtYXRpcXVlCmV4cG9ydCBtYXZlbl9zZXR0aW5nc19wYXRoPS9tbnQvbmFzX3VzaWwvYXBwcy9CdWlsZFRvb2xzL21hdmVuX3NldHRpbmdzL3NldHRpbmdzX3JlY2V0dGVfaW5mb3JtYXRpcXVlX2xpbnV4LnhtbApleHBvcnQgYnVpbGRfYW50X25hbWU9YnVpbGRfJHtSRUZFUkVOVElFTF9OQU1FfS54bWwK"}
Cannot understand how using api to download with curl my file on gitlab.
here my url:
https://gitlabxxxxxxx/administration/gitlab/raw/master/src/main/script/deploiement/settings/rci/setting.sh
I have checked official website but cannot understand how it's works and which part I should replace:
curl --request GET --header 'PRIVATE-TOKEN: <your_access_token>' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb/raw?ref=master'
I have tried but it's not working
curl --insecure --request GET --header 'PRIVATE-TOKEN: Y_F8YP3nUnFbzhxkQvgo' 'https://gitlabxxxxxxxxxx/api/v4/administration/gitlab/98/src/main/script/deploiement/settings/rci/sesameweb2.sh/app%2Fmodels%2Fkey%2Erb/raw?ref=master'
My project ID is : 98
I would like to download this setting.sh, I'am using gitlab-ce 12.6.4.
Thanks for help
When following the docs, you need to follow the /projects/:id/repository/files/:file_path format for the link, so don't replace everything, just the pieces with : in front.
You also need to make sure that the file path is using URI encoding.
With the information that you have, it would look something like this:
https://gitlabxxxxxxxxxx/api/v4/projects/98/repository/files/src%2Fmain%2Fscript%2Fdeploiement%2Fsettings%2Frci%2Fsetting.sh?ref=master
curl -k --request GET --header PRIVATE-TOKEN:"<PRIVATE-TOKEN>" https://<gitlab_url>/api/v4/projects/<id>/repository/files/<path_url_encoded>/raw?ref=master > <destination_path>
OR
refer official example:
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master"
Here:
PRIVATE-TOKEN - enter your private token
path_url_encoded = Check here
destination_path - path where you want to download the file with filename
Check the Official Doc

Curl show Content-Type only

I was wondering if it is possible to use curl to only show the content-type of the response header.
I want to check if the content-type is text/html for example before downloading instate of downloading the file and then find out it is application/pdf.
I used the example below in the hope that it would return the document if it is valid for me and else do nothing or something! The sample below just prints the full content of the page.
curl -F "type=text/html" www.google.nl
But If i do something like the example below it still downloads the whole thing, and I don't think that is right...
curl -F "type=text/html" http://www.axmag.com/download/pdfurl-guide.pdf
Many thanks :D
Option -F is for forms. Instead you want to send a HEAD request for retrieving only the response header without the response body by using option -I.
To display an URL's content type:
curl -s -I www.google.nl | grep -i "^Content-Type:"
Here option -s is added for silent mode for excluding the progress meter and error messages.
You can also specify the Accept header in your HTTP request. This header is used to accept only specific content types:
curl -s -H "Accept: text/html" http://www.axmag.com/download/pdfurl-guide.pdf
But the disadvantage is that most webservers will serve you an error page which also has the content type text/html. Hence you will still get a HTML file.
You can use the "-w" option too, with the "content-type" parameter:
curl -s -o /dev/null -w '%{content_type}' 'google.com'
Where:
-s: Silent mode, dont send any more to screen
-o: Output to file, and in this case, sends to /dev/null
-w: Where you show only with you want, in this case, content type
Reference: https://curl.haxx.se/docs/manpage.html

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