Linux curl put file

How to do a PUT request with cURL?

This example also uses the -d flag to provide arguments with your PUT request.

«man curl» on -X: «Normally you don’t need this option. All sorts of GET, HEAD, POST and PUT requests are rather invoked by using dedicated command line options.» But I couldn’t find another way.

As Martin C. Martin’s answer also changes to GET after a redirect from the server this is the more useful answer in my opinion.

Curl 7.47 and this would not work for me. theabraham’s answer always defaults to post behavior. —get (or -G) however will force the -d (—data) fields to become url parameters and does work.

Quick Answer:

In a single line, the curl command would be:

curl -X PUT -H "Content-Type: multipart/form-data;" -F "key1=val1" "YOUR_URI" 
curl -X PUT -H "Content-Type: application/json" -d '' "YOUR_URI" 
curl -X POST "YOUR_URI" -F 'file=@/file-path.csv' 

Alternative solution:

You can use the POSTMAN app from Chrome Store to get the equivalent cURL request. This is especially useful when writing more complicated requests.

For the request with other formats or for different clients like java, PHP, you can check out POSTMAN/comment below.

POSTMAN to get the request code

No idea why this has been downvoted. I copied here the idea how to pass JSON body for curl PUT. Also postman is pretty awesome tool to get curl code for more complicated queries 🙂

Читайте также:  Linux display on windows

Thanks !! Even I don’t have any idea why about the downvotes. Might be reluctance of the users to adopt it. I have created a respo of all my REST apis for mobile in POSTMAN and that is the best productivity tip that I can suggest to anyone working with REST.

Having to install a complete browser (if one doesn’t have Chrome) and an extension for it might be an overkill for some people imho. At least you could have provided an alternative for another more common (default installation) web browser like Firefox, where the HttpRequester does a similar job.

An example PUT following Martin C. Martin’s comment:

curl -T filename.txt http://www.example.com/dir/ 

With -T (same as —upload-file ) curl will use PUT for HTTP.

Unfortunately, the -T is no substitute for -X PUT if you want to specify parameters with -d or -F . -T sends the content of a file via PUT. To achieve the GET after a redirect, add the parameter —location

I am late to this thread, but I too had a similar requirement. Since my script was constructing the request for curl dynamically, I wanted a similar structure of the command across GET, POST and PUT.

Here is what works for me

For PUT request:

curl --request PUT --url http://localhost:8080/put --header 'content-type: application/x-www-form-urlencoded' --data 'bar=baz&foo=foo1' 

For POST request:

curl --request POST --url http://localhost:8080/post --header 'content-type: application/x-www-form-urlencoded' --data 'bar=baz&foo=foo1' 

For GET request:

curl --request GET --url 'http://localhost:8080/get?foo=bar&foz=baz' 

Источник

CURL cli PUT request — send data and file

I’m working on PUT method of API which will update entity defined by ID in URL. I need to send file and some additional data for user authentication (like signature and user public key). I fount out how I can send file:

curl -v -X PUT -T "file.epub" localhost:8080/api/book/?id=123 
curl -v -X PUT --data test=abc localhost:8080/api/book/?id=123 

2 Answers 2

curl -X PUT -F file=@file.epub -F test=abc localhost:8080/api/book/?id=123 

I am assuming you can change your API, so you can upload the file using a parameter, and in this example the parameter is called file .

Читайте также:  Time setting in linux command

Thanks for quick response. I don’t understand very well what you mean by «you can upload the file using a parameter, and in this example the parameter is called file». I won’t be able to initiate file uploading from user computer. Can you explain it in more details? Thanks.

I tried this PUT request. When I process data came with request in PHP script I got strange values. First of all I receive 18 parameters while I pass only 2. The name of parameter for example: ——————————e2ece971afa8\r\nContent-Disposition:_form-data;_name. And there is no «test» parameter. At the same time with request curl -v -X PUT —data test=abc localhost:8080/api/book/?id=123 I can retrieve parameter with right name and right value. It seems that we send file in a wrong way for PUT request.

Источник

cURL: PUT request examples

cURL HTTP PUT request examples with my most frequently used command-line options.

The examples for PUT are very similar to those detailed in my post explaining how to do POST requests in cURL.

HTTP PUT request

The most basic command you can execute with cURL is an HTTP PUT request without a body.

To tell cURL to use a PUT request method we can use the -X , —request command-line option, the following command will perform the request using the PUT verb and output the response body:

curl -X PUT https://blog.marcnuri.com

HTTP PUT request with data

If you want to send some data with the PUT request, you can use the -d , —data command-line option. When using the data option, cURL sends data just like your browser would when you fill an HTML form and press the submit button. In addition, cURL will automatically add the Content-Type header with an application/x-www-form-urlencoded value.

curl -X PUT -d "field=value&tool=curl" https://postman-echo.com/put

HTTP PUT request with JSON data

To send JSON content in the request body of your HTTP PUT request, you need to use the -d , —data options in combination with a custom Content-Type header ( -H , —header ).

curl -X PUT -d '' -H "Content-Type: application/json" https://postman-echo.com/put

JSON from file

In case you want to load the JSON content from a file instead of providing it via command-line, you can load it using the @ symbol followed by the path to the file that contains the JSON data:

curl -X PUT -d "@file-with-json.json" -H "Content-Type: application/json" https://postman-echo.com/put

Summary

These are some of the common cURL PUT HTTP request examples I use on a daily basis. I hope they may come in useful for you too.

Читайте также:  Мой ip адрес узнать линукс

References

Источник

Оцените статью
Adblock
detector