How to send a file in a url via curl? - http

I have an image in a URL like https://i.postimg.cc/GpDskmSG/sdssds.png and I want to send it over to be stored at nft.storage. Could I send it directly as a URL instead of a file from a path? I tried the below but it only stores the image URL.
curl -H "Authorization: Bearer eyJhbGciOiJIwetertyrtyUzI1NiIsInR5cCI6Ikp" -H "Content-Type: image/png" --data "https://i.postimg.cc/GpDskmSG/sdssds.png" --url "https://api.nft.storage/upload"
P/S: I'm testing on windows curl.

According to the API docs (https://nft.storage/api-docs/), it will accept multipart/form-data for this type of POST.
You can try using -F/--form instead, for which a good example exists on the curl man page (https://linux.die.net/man/1/curl):
curl -F "web=#index.html;type=text/html" url.com

Related

What does -d stand for in curl request?

I am trying to send this HTTP request in Postman application:
curl -v https://api.someurl.com/z1/lists \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: authorization" \
-d '{ "list_id": "DXVBDAD" }'
Any body knows what -d stands for? and where should I put it in Postman?
The documentation says this:
(HTTP) Sends the specified data in a POST request to the HTTP server[...]
So this will be the body of your POST request. In Postman you have to put it into the 'body' field. There select 'raw' and then select 'application/json'.
Because that's the Content-Type of your request, specified with -H.
The -d or --data option makes the curl command send data in POST request to the server. This option makes the curl command pass data to the server using content-type (JSON in your case) just as the browser does when a user submits a form.

Configure HTTP Post headers and body to send Twilio request

I am trying to get my custom built HTTP Post request to execute the following call to twilio
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxxxx/Messages.json' \
--data-urlencode 'To=+15558675309' \
--data-urlencode 'From=+15017250604' \
--data-urlencode 'Body=This is the ship that made the Kessel Run in fourteen parsecs?' \
-d 'MediaUrl=https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg' \
-u xxxxx:your_auth_token
My custom built request accepts an URL, headers and a body, similar to the HTTP Post form at Hurl.it.
How do I translate the portions
--data-urlencode
and
-d
into my URL, headers and body?
Example: The -u part I put into a header with "Authorization: Basic ", and it worked perfectly, the server recognises me. I just can't get it to recognise the From, To, Body parts.
Tks!
Twilio developer evangelist here.
Both the -d and --data-urlencode flags indicate adding data to the POST request body. The data also needs to be url encoded. When the request is made, all the url encoded data is concatenated with ampersands and sent as the body.
In the case of your example, the POST request body would end up looking like this:
To=%2B15558675309&From=%2B15017250604&Body=This%20is%20the%20ship%20that%20made%20the%20Kessel%20Run%20in%20fourteen%20parsecs%3F&MediaUrl=https%3A%2F%2Fc1.staticflickr.com%2F3%2F2899%2F14341091933_1e92e62d12_b.jpg
Let me know if that helps at all.

WordPress 4.7 REST API Example Request does not work

I'm using successfully the WordPress 4.7 REST API in this way:
curl -H "Authorization: Basic mykey" -X POST --data-urlencode "title=Something" www.myhost.com/wp-json/wp/v2/posts/770
The example in the new docs suggests:
curl -X POST http://demo.wp-api.org/wp-json -d '{"title":"My New Title"}'
which obvious is a wrong endpoint. Adapting to
curl -H "Authorization: Basic mykey" -X POST www.myhost.com/wp-json/wp/v2/posts/770 --data-urlencode '{"title":"My New Title"}'
does not change the title but simply returns the post as JSON
Any idea?
Came over my own question, which I did resolve a long time ago. Just if someone struggles with this too.
First of course, posting data with curl requires the -d option an not the --data-urlencode. Second, the REST endpoint needs to know the type of data. Here JSON. So setting the correct Content Type is essential.
This is a complete example:
curl -H "Authorization: Basic mykey>" -H "Content-Type: application/json" -X POST -d '{"title":"Something"}' www.myhost.com/wp-json/wp/v2/posts/770

Pass SSOTokenID as set-cookie value in OpenAM

I'm using openam OAuth/OpenID for user authentication. As mentioned in the documentations, I could get SSOTokenID as a JSON object by making following HTTP request.
curl -X POST -H "X-OpenAM-Username: demo" -H "X-OpenAM-Password: changeit" -H "Content-Type: application/json" -d '' -k -v https://openam.example.com:8443/openam/json/authenticate?realm=/
Instead of that, I want to get SSOTokenID as the Set-Cookie header value of the HTTP response. Are there anyway that i can do it?
Assuming you are only using an authentication module that accepts a NameCallback and PasswordCallback (as you used in your example), then you can just use the legacy UI zero-page login , you need to disable XUI though
Using your example
curl -X POST -d 'IDToken1=demo&IDToken2=changeit' -k -v https://openam.example.com:8443/openam/UI/Login?realm=/

Send request to cURL with post data sourced 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. 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'

Resources