Saturday, March 2, 2024
HomeJava10 Examples of cURL command in UNIX and Linux

10 Examples of cURL command in UNIX and Linux


Hi there guys, if you wish to be taught concerning the cURL command and in search of an superior, hands-on tutorial then you might have come to the suitable place. Earlier, I’ve shared many examples based mostly tutorials to be taught many important Linux instructions discover, grep, lsof, and chmod command and on this article, I’m going to share 10 examples of curl command in Linux. The curl is likely one of the important instructions to ship HTTP requests from UNIX and Linux working programs. curl command is a part of the cURL package deal and it isn’t simply helpful to ship HTTP requests but additionally lets you switch information utilizing FTP and ship mail utilizing SMTP.
It additionally helps SSL certificates, HTTP POST, HTTP PUT, FTP importing, HTTP kind based mostly add, proxies, HTTP/2, cookies, consumer+password authentication (Fundamental, Plain, Digest, CRAM-MD5, NTLM, Negotiate, and Kerberos), file switch resume, proxy tunneling and extra. It is significantly helpful if you’re working with net companies.
You should use the curl command to name net companies proper from the UNIX command immediate, obtain a response, test whether or not your server is wholesome or not. You’ll be able to write testing and monitoring scripts utilizing the curl command. It is much like the wget command, which can also be used for HTTP requests nevertheless it’s far more highly effective and helps many protocols.

The curl command is by default accessible in lots of UNIX set up but when it isn’t you may all the time obtain the most recent model from http://curl.haxx.se/obtain.html, the most recent model of the cURL package deal is curl 7.43.0, launched on seventeenth June 2015. cURL has completely different packages accessible for various UNIX flavors like Linux, Debian, Fedora, ArchLinux, AIX, FreeBSD, Suse, Redhat, Ubuntu, and even for Mac OS X and Apple iOS.

10 Instance of curl command in Linux

Listed here are a few of the helpful examples of curl command in Linux. You should use this command to check your REST API from the Linux command line. You too can test in case your net utility is up and down through the use of the curl command in a script after which working it from crontab or a scheduling utility like Autosys.

1) How you can ship an HTTP request from UNIX

You should use curl or wget to ship an HTTP request from UNIX, as proven under:

$ curl http://google.com

By default, curl makes use of the GET methodology to ship HTTP requests. You too can use question parameters whereas connecting to net companies utilizing HTTP as proven under:

$ curl http://api.openweathermap.org/knowledge/2.5/climate?id=2172797

Remember to incorporate URL inside single quotes if you happen to embody a couple of question parameter. Why? as a result of a number of question parameters are separated utilizing & which has particular that means within the shell, to run the command within the background, by together with it inside single quotes we use it actually, as proven under:

$ curl 'http://api.openweathermap.org/knowledge/2.5/climate?lat=35&lon=139'

2) How you can present timeout for HTTP request in UNIX

One other fascinating instance of a curl command is through the use of the -m choice. You should use the curl -m choice to supply a timeout for HTTP requests. If the server won’t return any response inside a specified time interval then curl will exit, as proven under

$ curl -m 3 'http://api.openweathermap.org/knowledge/2.5/climate?lat=35&lon=139'

will look ahead to 3 seconds earlier than timing out. BTW, -m is used for a few issues in curl e.g. for the utmost variety of redirects and most file dimension to obtain.

Alternatively, you should utilize extra verbose –max-time, –max-redirs, and –max-filesize choices. Value noting is the utmost time allowed for switch is in seconds, which implies -m 5 means 5 seconds. You should use this selection to test whether or not your web site or net service is responsive or not.

How to use curl Command in UNIX and Linux



3) How you can ship HTTP POST request from Linux

You’ll be able to ship any kind of HTTP request through the use of the curl -X command in Linux. It lets you specify HTTP strategies aside from GET, for instance, the next command will ship an HTTP POST request from the Linux command line:

$ curl -X POST http://api.openweathermap.org

Since GET is the default methodology, you need not specify it however if you wish to ship different HTTP strategies like PUT or POST, you should utilize this selection. Btw, what’s using POST request with out sending any knowledge? Let’s have a look at our subsequent instance, which lets you ship knowledge to the server utilizing the curl command.

4) How you can ship knowledge utilizing HTTP POST in UNIX

You too can ship a POST request utilizing the curl -d choice. -d is nothing however for knowledge, which is distributed as POST physique. The information is predicted to be URL-encoded.

$ curl -d 'lat=35&lon=139' http://api.openweathermap.org/knowledge/2.5/climate

It will ship question parameters as a POST request. You too can use the -d choice a number of instances to specify completely different knowledge items like

$ curl -d lat=35 -d lon=139 http://api.openweathermap.org/knowledge/2.5/climate

It will ultimately be mixed as -d ‘lat=35&lon=139’. The -d choice can be used to submit HTML kind knowledge to the server. 

5) How you can ship a file by way of HTTP POST in Linux

In case your knowledge is massive, you may hold them inside a file and inform curl the file identify. It can switch all that knowledge utilizing HTTP publish request to the server, right here is an instance of sending a file utilizing curl:

$ curl -d @requestData.txt http://api.openweathermap.org/knowledge/2.5/climate

The @ signal tells curl that no matter follows is a file identify. BTW, the content material of the file should be URL encoded. You too can use –data as a substitute of -d if you happen to want probably the most verbose choice.

6) How you can ship username password for authentication utilizing curl

You should use the curl -u choice to cross username and password for authentication. The -u choice is a shortcut of –user choice which specifies username and password for server authentication. Should you utilizing Twitter, right here is how one can replace your standing utilizing the curl command proper from UNIX:

$ curl -u username:password -d standing='curl is nice' 
http://twitter.com/statuses/replace.xml

Should you do not want your password to be saved within the shell historical past, you may omit the password half and curl will immediate for a password when it tries to authenticate on the server. As I instructed earlier, -d instructs curl to make use of the HTTP POST methodology to ship a request, and “standing=..” will probably be despatched as a part of the request physique. 

7) How you can specify HTTP header utilizing curl command in Linux

You’ll be able to specify the HTTP header through the use of the curl -H choice. Since with net service you typically take care of both JSON or XML quite than HTML, it is sensible to specify content-type as both “utility/json” or “utility/xml”. You are able to do this utilizing curl as proven within the following instance:

$ curl -H "Settle for: utility/json" 
'http://api.openweathermap.org/knowledge/2.5/climate?lat=35&lon=139'

You too can use headers whereas sending knowledge to the server utilizing HTTP publish e.g.

$ curl -X PUT 
-H 'Content material-Sort: utility/json' 
-d '{"id":"101", "description":"child cleaning soap"}'
http://localhost:8080/merchandise/add

One aspect tip, you may cut up your UNIX command into a number of strains through the use of (backslash). It makes your command extra readable, particularly if it is getting lengthy.

8) How you can view HTTP response header in Linux

You should use the curl -i command to view the response header in UNIX. Since API and Internet Service supplier is more and more utilizing HTTP header to supply details about caching, content-type, authorization, and so on, it’s totally helpful to see HTTP header utilizing curl as proven under:

$ curl -i http://api.openweathermap.org/knowledge/2.5/climate?zip=94040,us

It will return all response headers from HTTP response as proven under:

10 Examples of curl Command in UNIX and Linux

That is all about use the curl command to ship HTTP requests and obtain a response. We have now seen a number of examples of curl instructions starting from sending easy HTTP requests to timeout, with question parameters and several types of HTTP requests like POST, PUT, and DELETE.

It is truly a must-know software if you’re working with REST net companies that return JSON or XML output. You’ll be able to check your net companies proper from the UNIX field, can construct scripts to check and monitor them as nicely. As a Java developer, I exploit the curl lots after I work with net services-based purposes.


Different Linux command examples, you might prefer to discover

  • 10 examples of grep command in Linux (see)
  • 10 examples of sed command in UNIX (see)
  • 10 examples of chmod command in Linux (see)
  • 10 examples of xargs command in Linux (see)
  • 10 Important Networking instructions in Linux (see)
  • 10 Examples of tar command in Linux (see)
  • 10 examples of the networking command in Unix (instance)
  • My favourite Linux programs for rookies (Linux on-line programs)
  • 6 Free On-line course to be taught Bash (free course)
  • 5 Instance of kill instructions in Unix and Linux (instance)
  • High 5 Bash Scripting course for Learners (bash programs)
  • High 10 Programs to Study Linux instructions (programs)
  • VI Editor examples and suggestions for rookies (instance)
  • How does nslookup command work in UNIX? (reply)
  • 10 books to be taught important instructions of Linux or UNIX? (record)
  • How you can use the CUT command in Linux (tutorial)
  • How you can exit from the telnet command in UNIX? (instance)



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments