Saturday, May 4, 2024
HomeJavaMethods to name REST API an ship HTTP GET and POST Request...

Methods to name REST API an ship HTTP GET and POST Request utilizing cURL command in Linux? Instance Tutorial


The curl or cURL command of Linux is a compelling and versatile command which lets you ship refined HTTP requests proper out of your Linux command line window. You should utilize the cURL command to check your RESTful Net Providers by sending GET and POST requests, doing authentication, saving a cookie within the file, and so on. The curl command is, the truth is, the go-to instrument for a lot of Java and Spring builders working in internet purposes and consuming knowledge from the secured RESTful Net Providers. Nonetheless, it’s also possible to use it to check your easy REST Net APIs with out safety. I’ve used it many occasions to test if our Net service is up or not, or is there any error whereas accessing these providers.

You too can use this to write down a shell script for mechanically doing health-check to your atmosphere. All you’ll want to do is to schedule the shell script utilizing the 
crontab command, and it’ll mechanically ship a request and report errors or success at a hard and fast time of day.

Listed here are a number of the helpful command-line choices of the cURL command, which is extraordinarily essential for connecting and testing your RESTful Net Service:

[--digest] it's a digest authentication
[-u{username}:{password}] attaching username and password
[-X PUT] technique="put"
[-H 'Expect: '] header = 'Count on: '
[-H 'Content-type: application/xml'] extra header

The syntax of the curl command is as follows:

$ curl --digest 
-u{username}:{password} 
-v 
-X PUT 
-H 'Count on: ' 
-H 'Content material-type: software/xml' 
-d @- 
http://localhost:8080/SpringRestDemo/api/guide/9783827321312 
< knowledge.xml 

Right here, d is for knowledge. The “-d@ -“ possibility will instruct the curl command to ship a POST request with the info it reads from commonplace enter. The ‘<‘ operator tells the shell to feed a file to stdin. You may make it less complicated by doing -d @knowledge.xml and never utilizing commonplace enter in any respect.

If you’re new to Linux, I recommend you undergo Study Linux in 5 Days and Stage Up Your Profession course on Udemy to get a head-start and be taught some fundamentals + important Linux instructions you’ll going to make use of daily. It is an superior course and really inexpensive too. I purchased in simply $10 on Udemy’s flash sale, which occurs now and again.

Methods to ship HTTP request from the Linux command line utilizing cURL command? 

Right here is my record of a number of the most helpful examples of curl command, which I take advantage of in my day-to-day life to check RESTful internet providers from the command line. You may even use these to write down scripts and run them from crontab to mechanically take a look at the provision of your RESTful API, very helpful if you’re supporting a manufacturing REST software.

1. Methods to take a look at Authentication towards REST API

Suppose the URL for login in your REST internet service is http://localhost:8080/SpringRestDemo/j_spring_security_check then you should use the next curl command for performing login:

$ curl -i -X POST 
          -d j_username=person 
          -d j_password=password
             http:

This request can even return the Cookie, which can then be utilized by any subsequent request towards the identical REST Net Service.

Btw, I’ve used Spring safety to guard our RESTful internet service right here, if you wish to be taught extra about that, you possibly can test Study Spring Safety: The Certification Class by Eugen Paraschiv of Baeldung.   It is one of many higher programs to be taught Spring Safety, which supplies a guided and code-focused tour of Spring Safety.

2. Saving the Cookie in a file utilizing the curl command

Whereas authenticating towards RESTful Net Service, If you’d like, it’s also possible to save the Cookie into a particular file utilizing the curl command as proven under:

$ curl -i -X POST 
          -d j_username=person 
          -d j_password=password 
          -c /tmp/cookies.txt
           http:

This request will save the Cookie into /tmp/cookies.txt file.

You too can save the Cookie into your private home listing in case you like; it will likely be the identical there as /tmp is usually accessible to all people, and likewise it is often cleaned by Linux.

3. Attaching Header and Cookie into an HTTP request utilizing curl

You too can connect HTTP headers utilizing the curl command through the use of possibility –header for authentication and authorization functions. You may even connect Cookie into your HTTP request utilizing the -b command possibility as proven under:

$ curl -i --header "Settle for:software/json" 
          -X GET 
          -b /tmp/cookies.txt
           http:

This request will connect the “Settle for” header and earlier saved cookie from /tmp/cookies.txt file into your HTTP request. The response will appear like under:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content material-Kind: software/json;charset=UTF-8
Switch-Encoding: chunked
Date: Tue, 06 Jun 2017 22:21:23 IST
[{“ISBN”:9783827321312,”title”:”Effective Java”}]

If you’re not very accustomed to HTTP however at present engaged on a undertaking which has a RESTful internet service, I strongly recommend you first undergo the HTTP Fundamentals course at Pluralsight. It is a free course and offers you adequate data about HTTP to outlive in your day job whereas working with HTTP and REST.

testing RESTful web service from command line in UNIX

4. Accessing RESTful Net Service 

In case your RESTful Net Service does not implement safety, then you possibly can entry a useful resource utilizing the curl command as proven under:

$ curl -i http://localhost:8080/SpringRestDemo/api/guide/9783827321312

This may return the JSON illustration of the guide with ISBN 9783827321312, but when your REST API is secured e.g. through the use of http fundamental auth, then you’ll obtain a 403 unauthorized response as proven under:

HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=25A833C16B6530A007CFA3AECEE2216B; Path=/SpringRestDemo/; HttpOnly
WWW-Authenticate: Fundamental realm=”Safe REST API”
Content material-Kind: textual content/html;charset=utf-8
Content material-Size: 1028
Date: Tue, 06 Jun 2017 22:21:23 IST

When you take a look at the identical REST API utilizing a browser, you may be prompted to enter username and password by the browser as a result of it would use HTTP fundamental authentication, however curl will not do this. It’s good to specifically present its username and password, as proven within the subsequent instance. 

See RESTful Net Providers, Java, Spring Boot, Spring MVC, and JPA course on Udemy to be taught extra about HTTP fundamental and digest authentication in REST, it is a free course, and you’ll end it in a few hours.

5. Accessing Safe RESTful Net Service utilizing username and password

You should utilize the –user command-line possibility of curl to ship username and password together with HTTP request to entry a safe REST internet API as proven under:

$ curl -i --person username password 
          http://localhost:8080/SpringRestDemo/api/guide/9783827321312

Now, you’ll obtain an HTTP response with success code 200, together with a cookie. That is helpful when your software is utilizing a username and password saved in a database or flat file for login.

It may also be used along side LDAP-based authentication, so long as you simply want to offer a username and password.

6. Enabling digest authentication utilizing the curl command

In case your REST API is secured utilizing digest authentication, then you should use the –digest flag to allow HTTP digest authentication within the curl command as effectively.

$ curl --digest 
       --user username:password
        -i http://localhost:8080/SpringRestDemo/api/guide/9783827

Btw, if you’re interested by the way to safe your API utilizing digest authentication, effectively, you should use Spring safety. It helps each HTTP fundamental and digest authentication. You may see Spring Safety Core: Newbie to Guru course by John Thompson on Udemy for implementation particulars.

7. Setting a couple of header within the curl command

If you’d like you possibly can set a couple of HTTP header in your HTTP request through the use of the -H command-line possibility twice utilizing curl in UNIX, as proven under:

$ curl -H "Settle for: software/json"
       -H 'If-None-Match: "12334dfsfsdffe004fsdfds36a6"'
       -i http:

You may see that through the use of -H choices, it’s also possible to use attache HTTP headers to your requests. It is a highly effective characteristic to check superior options of REST API from the Linux command line. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments