Linux upload file curl

How to upload file / image using CURL command line ?

If you want to upload some file or image from ubuntu curl command line utility, its very easy !

CURL provides a simplest form of syntax for uploading files, “-F” option available with curl emulates a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data.

Uploading single file / image

Lets say, we want to upload an picture available at “/home/myuser/mypicture.jpg” to server at “https://www.YOUR_SERVER_URL/upload” which uses file input form with parameter name as “image”, then we will have to use below command,

$ curl -k -X POST -F 'image=@/home/myuser/mypicture.jpg' -v https://www.YOUR_SERVER_URL/upload/

here, above -v option is just for debugging purpose to understand how it goes with more verbose messages, you can avoid using it in production.

Uploading multiple files

For uploading multiple files, just add another “-F” option along with the same command as above, like

$ curl -k -X POST -F 'image=@/home/myuser/mypicture1.jpg' -F 'image=@/home/myuser/mypicture2.jpg' -v https://www.YOUR_SERVER_URL/upload/

“-F” option with curl command enables uploading of binary files etc.

To force the ‘content’ part to be a file, prefix the file name with an @ sign.

To just get the content part from a file, prefix the file name with the symbol

Example, to send your password file to the server, where ‘password’ is the name of the form-field to which /etc/passwd will be the input: curl -F password=@/etc/passwd www.mypasswords.com

You can also tell curl what Content-Type to use by using ‘type=’, in a manner similar to: curl -F “web=@index.html;type=text/html” url.com or curl -F “name=daniel;type=text/foo” url.com

You can also explicitly change the name field of a file upload part by setting filename=, like this: curl -F “file=@localfile;filename=nameinpost” url.com If filename/path contains ‘,’ or ‘;’, it must be quoted by double-quotes like: curl -F “file=@\”localfile\”;filename=\”nameinpost\”” url.com or curl -F ‘file=@”localfile”;filename=”nameinpost”‘ url.com Note that if a filename/path is quoted by double-quotes, any double-quote or backslash within the filename must be escaped by backslash.

Источник

cURL http post file upload using curl —data in linux command line

but it is not uploading the file to the server, even though it is sending the response back. Although if I use this command it will upload the file and send the response back:

curl -v -F 'filename=registration.png' -F 'file=@/var/www/fbapp/images/registration.png' http://myserver/xmltest/curlupload.php 

But I wanted the command in the curl —data format itself. This is my PHP code in the server:

> POST /xmltest/curlupload.php HTTP/1.1 > User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 > Host: myserver > Accept: */* > Content-Length: 44 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 44out of 44 bytes < HTTP/1.1 200 OK < Date: Thu, 06 Nov 2014 07:01:56 GMT < Server: Apache < Vary: Accept-Encoding < Transfer-Encoding: chunked < Content-Type: text/html < * Connection #0 to host myserver left intact * Closing connection #0 filedata= success 

1 Answer 1

You usually can't simply pick -F or -d (--data) at your choice. The web server that will receive your post expects one of the formats. If the form you're trying to submit uses the type 'multipart/form-data', then and only then you must use the -F type. If not, you should use -d which then causes a posting with the type 'application/x-www-form-urlencoded'.

Читайте также:  Linux get battery info

multipart/formposts with -F uses a special formatting of the post with Mime-headers separating the different parts and each part having its own set of headers.

-d is just raw data being sent for the server to interpret/decode.

(But since you write the PHP code in this case, you just have to decide which POST method you want to accept and then make your curl command line use that.)

Источник

Learn to Use CURL Command in Linux With These Examples

Learn some essential and some pro tips and tricks about using the Curl command in Linux.

What is CURL ?

CURL is a tool for data transfer. It is also available as a library for developers and as a CLI for terminal-based use cases. Both have the same engine inside (Truth is that CLI tool is just the program that uses the library under the hood).

CURL works with every protocol you might have used. Head over this site to check whether CURL works with your target protocol or not.

What CURL can do?

Hmm… Everything that is related to data transfer. Everyone must have used a browser. Even now, you are reading this article through your browser. What browser does, it requests a page and gets it as a response. It can write and read cookies. And then it renders(displaying the content, images and executing JS scripts) it.

CURL can do everything a browser except for the last part rendering because it is not related to data transfer.

As wrap up, CURL can download HTML pages, fill HTML forms and submit them, download files from a FTP/HTTP server and upload files to the same and read/write cookies.

This makes it an excellent tool to be used in scripting, debugging and forensic analysis etc.

Curl command examples

Let’s see what can you do with Curl.

1. Get a response from a server

Everything from server is a response to the request. So getting a HTML page is same as downloading a file.

To get a HTML response from http://info.cern.c,

To get the list of posts as a response from a server ( https://jsonplaceholder.typicode.com/posts),

curl https://jsonplaceholder.typicode.com/posts

Since we know how to get a response from a server, you can download a file ( say Google logo ).

curl https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

Above command will dump binary image data which you can’t view in the terminal. You need to save them and then use a photo viewer to see them.

Note that various option flags can be placed anywhere on the command instead of the strict ordering. So no worry if you placed any option in the last while the examples had the flag in the beginning.

2. Save the file with a default file name

Every file that is served on the internet has a filename. To use the same filename as the downloaded filename use -O flag.

curl -O http://www.google.com/robots.txt

3. Save the file with custom name

To save the filename with your own custom name, use -o flag followed (strictly) by a custom name.

curl -O http://www.google.com/robots.txt googleRobots.txt

4. Download multiple files

To download multiple files, separate them with a white space.

Читайте также:  Установка времени arch linux

If you want to use -O flag for all the URL’s, use

curl url1 url2 url3 -O -O -O 

The same workaround should be done for any flag. This is because the first occurrence of a certain flag is for the first URL, the second flag is for the second URL and so on.

5. Download a range of files

curl has the in-built ability to download a range of files from the server. This can be illustrated from the following example.

curl http://www.google.com/logo/logo8.png

Above command downloads files from logo1.png, logo2.png, logo3.png and up to logo9.png.

6. Download a file only if latest

To download a file only if the file’s modification time is latest than the given time.

curl url -z "DD MMM YY MM:HH:SS"

7. Resume Downloading

If you have already partially transferred a file, you can resume the transfer by using the -C flag. Offset from which transfer needs to be continued should be passed as a parameter to the -C flag.

curl -C 1024 http://seeni.linuxhandbook.org/files/largeFile.mpv -O

8. Upload a file

To upload a file to the server, one needs to use -T flag followed by the file path on your local system.

curl -T uploadFile.txt http://upload.linuxhandbook.org/files

9. Delete a file

To delete a file named deleteFile.txt in a server, one can use -X flag which is intended for any HTTP verb/method(like GET, POST, PUT, DELETE, PATCH). Most of the FTP servers will have configured DELETE method if not all advanced HTTP methods.

curl -X DELETE http://upload.linuxhandbook.org/files/deleteFile.txt

You can also modify the above command for any HTTP method to do the corresponding task. For Example, if your server allows TRUNCATE method ( this is made-up HTTP method, not a standard one) which removes only the content in the file and not the file, one can use the command similar to the below one.

curl -X TRUNCATE http://upload.linuxhandbook.org/files/mysql.dump

Above mentioned are the main uses of curl. But there might be difficulties which needed to be fought such as redirects, user authentication, SSL certificates, etc., We can call them add-ons as they are only optional but still remain crucial for certain purposes. Let’s see some of those addons and how to handle it with curl in the next section.

10. Avoid redirects

When you request http://www.google.com , you will be served only the regional page such as www.google.co.in. This is done with the help of redirects (HTTP packets with status codes in the range 300-399).

You can avoid redirects with the option L.

11. Authentication

When the server is configured to serve for only certain individuals with credentials, they will be provided with username and password. One can make login with the help of -u flag.

curl -u username:password http://seeni.linuxhandbook.org/files/tasks.txt

12. Limit data transfer

If you want to impose a data transfer limit use –limit-rate flag. Following command tries to download the file with rate limit as 10K.

curl --limit-rate 10K http://seeni.linuxhandbook.org/files/logoDetails.tgz

13. Show/Hide transfer Status

If the response is redirected from the terminal such as downloading, uploading then curl automatically shows the status/progress meter for the transfer.

Читайте также:  Компиляция cpp на linux

If you do not want to see the progress meter, just append the command with -s flag. Progress will not be shown for response directed for the terminal.

14. Ignore SSL certificates

Do you remember the situations in which you need to give security certificate exception to visit some websites? If you trust the sources and you want to do a data transfer, you can ignore SSL certificate validation by using -k flag.

curl -k https://notSoSecure.org/files/logoDetails.tgz

15. Get Header Information also

To display the header information along with transferred data, use the -i flag.

curl -i http://www.google.com/robots.txt

16. Get Header information Only

If you want only the headers and not the data, use the -I flag

curl -I http://www.google.com/robots.txt

17. Change User Agent

Some websites and servers don’t allow certain kinds of devices to access their systems. But how do they know that we are using a specific kind of device? This is due to the User-Agent HTTP header field. We can change this User Agent with -A flag.

curl -A "Mozilla FireFox(42.0)" http://notAllowedForCLI.sites.org/randomFile.png

18. Sending data to the Server

If the server needs some data such as token or API key, use -d flag to send the data. Data that needs to be sent should follow the flag in the command. One can use “&” to combine multiple data. This is usually done by GET and POST requests in browsers. This is one of the ways by which you can send your form information.

curl -d "token=34343abvfgh&name='seeni'" http://api.restful.org/getcontent

19. Write Cookies to a File

Cookies are some small information that allows maintaining a session with a stateless HTTP protocol. If you want to know more about Cookies, refer to this great resource.

To write cookies to a file, -c flag followed by the cookie filename should be used.

curl -c googleCookie.txt http://www.google.com/files

20. Reading Cookies from a File

To read a cookie from the file, -b flag followed by cookie filename can be used.

curl -b googleCookie.txt http://www.google.com/files

Note that -b flag only reads the cookie from the file. So if the server resends another cookie, you might need to use -c option to write them.

21. Start a new Session

If you want to initiate a new session by discarding the cookies, use -j flag. It starts a new session even if you have provided the cookie file to read with -b flag.

curl -b googleCookie.txt http://www.google.com/files -j

Congratulations! You made it to the end. If you find this article useful, share it with your friends and follow us on Social media. If you have any suggestions about this article or any other topic, feel free to drop them below.

Источник

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