How to exam RESTful Web Services using curlicue ascendency inward Linux

The curl control of Linux is real powerful together with versatile control which allows you lot to post sophisticated HTTP asking correct from Linux control line. You tin utilization curl to bear witness your RESTful Web Services past times sending GET together with POST request, doing authentication, saving a cookie inward the file etc. The gyre control is the go-to tool for many Java together with Spring developers for consuming the secured RESTful Web Services, but you lot tin also utilization it bear witness your uncomplicated REST Web APIs without security.

Here are only about of the useful control business options of curl command, which is extremely of import for connecting together with testing your RESTful Web Service:

[--digest] it's a digest authentication
[-u{username}:{password}] attaching username together with password
[-X PUT] method="put"
[-H 'Expect: '] header = 'Expect: '
[-H 'Content-type: application/xml'] additional header

The syntax of gyre control is every bit follows:

$ gyre --digest \
-u{username}:{password} \
-v \
-X PUT \
-H 'Expect: ' \
-H 'Content-type: application/xml' \
-d @- \
http://localhost:8080/SpringRestDemo/api/book/9783827321312 \
< data.xml 

Here, d is for data. The "-d@ -" choice volition instruct gyre control to post a POST request alongside the information it reads from measure input. The '<' operator tells the musical rhythm out to feed a file to stdin. You tin arrive simpler past times doing -d @data.xml together with non utilization measure input at all.



Testing RESTful Web Services from control business inward Linux/UNIX

Here is my listing of only about of the most useful examples of gyre command, which I utilization inward my twenty-four hr catamenia to twenty-four hr catamenia life to bear witness RESTful spider web services from control line. You tin fifty-fifty utilization these to write scripts together with run it from crontab to automatically bear witness the availability of your RESTful API, real useful if you lot are supporting a production REST application.


1) How to bear witness authentication against REST API
Suppose the URL for login inward your REST spider web service is http://localhost:8080/SpringRestDemo/j_spring_security_check together with hence you lot tin utilization the next gyre control for performing login:

$ gyre -i -X POST -d j_username=user -d j_password=password
http://localhost:8080/SpringRestDemo/j_spring_security_check

This asking volition also render the Cookie which volition together with hence live used past times whatsoever subsequent asking against the same REST Web Service. Btw, nosotros bring used Spring safety to protect our RESTful spider web service here, if you lot desire to larn to a greater extent than most that, See Spring Security Fundamentals from Pluralsight, you lot tin acquire it for costless every bit Pluralsight gives a costless 10-day trial, which is plenty to larn Spring framework itself.


2) Saving the cookie inward a file using gyre command
While authenticating against RESTful Web Service, If you lot want, you lot tin also salvage the cookie into a specific file using gyre control every bit shown below:

$ curl -i -X POST -d j_username=user -d j_password=password -c /tmp/cookies.txt
http://localhost:8080/SpringRestDemo/j_spring_security_check

This asking volition salvage the Cookie into /tmp/cookies.txt file. You tin also salvage the cookie into your habitation directory if you lot like, it volition live same in that place every bit /tmp is by together with large accessible to everybody together with also it's oft cleaned past times Linux.




3) Attaching header together with Cookie into HTTP asking using Curl
You tin also attach HTTP headers using curl control past times using option --header for authentication together with ascendance purposes. You tin fifty-fifty attach cookie into your HTTP request using -b control choice every bit shown below:

$ curl -i --header "Accept:application/json" -X GET -b /tmp/cookies.txt
http://localhost:8080/SpringRestDemo/j_spring_security_check


This asking volition attach "Accept" header together with before saved cookie from /tmp/cookies.txt file into your HTTP request. The response volition hold off similar below:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 06 Jun 2017 22:21:23 IST
[{"ISBN":9783827321312,"title":"Effective Java"}]

If you lot are non real familiar alongside HTTP but currently working on a projection which has RESTful spider web service, I strongly propose you lot to get-go become through HTTP Fundamentals course at Pluralsight, it's a costless course of teaching together with give you lot plenty noesis most HTTP to locomote inward your twenty-four hr catamenia task land working alongside HTTP together with REST.

 control of Linux is real powerful together with versatile control which allows you lot to post sophisti How to bear witness RESTful Web Services using gyre control inward Linux



4) Accessing RESTful Web Service 
If your RESTful Web Service doesn't implement safety together with hence you lot tin access a resources using curl control every bit shown below:

$ gyre -i http://localhost:8080/SpringRestDemo/api/book/9783827321312

This volition render the JSON representation of majority alongside ISBN 9783827321312, but if your REST API is secured e.g. past times using http basic auth, together with hence you lot volition have a 403 unauthrorized response every bit shown below:

HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=25A833C16B6530A007CFA3AECEE2216B; Path=/SpringRestDemo/; HttpOnly
WWW-Authenticate: Basic realm="Secure REST API"
Content-Type: text/html;charset=utf-8
Content-Length: 1028
Date: Tue, 06 Jun 2017 22:21:23 IST

If you lot bear witness the same REST API using a browser you lot volition live prompted to locomote into username together with password past times the browser because it volition utilization HTTP basic authentication, but gyre won't create that. You demand to peculiarly furnish it username together with password every bit shown inward adjacent example. See REST Fundamentals By Howard Dierking to larn to a greater extent than most HTTP basic together with digest authentication inward REST, it's a costless course of teaching together with you lot tin complete it a yoke of hours.



4) Accessing Secure RESTful Web Service using username together with Password
You tin utilization the --user control business choice of gyre to post username together with password along alongside HTTP asking to access a secure REST spider web API every bit shown below:

$ gyre -i --user username:password http://localhost:8080/SpringRestDemo/api/book/9783827321312

Now, you lot volition have an HTTP response alongside success code 200, along alongside a cookie. This is useful when your application is using username together with password stored inward a database or apartment file for login. It tin also live used inward conjunction alongside LDAP based authentication, every bit long every bit you lot only demand to furnish username together with password.

 control of Linux is real powerful together with versatile control which allows you lot to post sophisti How to bear witness RESTful Web Services using gyre control inward Linux

5) Enabling digest authentication using gyre command
If your REST API is secured using digest authentication together with hence you lot tin utilization --digest flag to enable HTTP digest authentication inward gyre control every bit well.

$ gyre --digest 
       --user username:password
        -i
 http://localhost:8080/SpringRestDemo/api/book/9783827

Btw, if you lot are curious most how to secure your API using digest authentication, well, you lot tin utilization Spring security. It supports both HTTP basic together with digest authentication. You tin encounter Learn Spring Security for implementation details.


6) Setting to a greater extent than than 1 header inward gyre command
If you lot desire you lot tin laid upward to a greater extent than than 1 HTTP header inward your HTTP asking past times using -H control business choice twice using gyre inward UNIX, every bit shown below:

$ gyre -H "Accept: application/json"
       -H 'If-None-Match: "12334dfsfsdffe004fsdfds36a6"'
       -i http://localhost:8080/SpringRestDemo


That's all most how to utilization gyre control to bear witness RESTful Web Services. You tin run these gyre control from Linux control business window correct from the server or past times using Putty or Cygwin from your evolution machine.

Further Learning
REST API Automation testing from scratch-(REST Assured java)
Linux Command Line Interface (CLI) Fundamentals
RESTFul Services inward Java using Bailiwick of Jersey By Bryan Hansen


Other REST and Linux control tutorials you lot may like
  • How does nslookup control locomote inward UNIX? (answer)
  • 10 examples of lsof control inward Linux? (examples)
  • 5 Books to larn RESTful Web Services  (list)
  • When to utilization PUT or POST inward REST? (answer)
  • 10 Frequently asked RESTful Web Service Interview Questions (list)
  • How to utilization the netstat control to notice which procedure is listening on a port? (example)
  • A Practical Guide to Linux Commands, Editors, together with Shell Programming (guide)

Thanks for reading this article, if you lot similar this article together with hence delight part alongside your friends together with colleagues. If you lot bring whatsoever proffer or feedback together with hence delight drib a comment.

Subscribe to receive free email updates:

0 Response to "How to exam RESTful Web Services using curlicue ascendency inward Linux"

Posting Komentar