Seven Linux Curlicue Ascendancy Examples For Testing Restful Spider Web Services From Ascendancy Line

The curl or cURL ascendance of Linux is a real powerful as well as versatile ascendance which allows you lot to shipping sophisticated HTTP asking correct from your Linux ascendance business window. You tin sack utilisation the cURL ascendance to assay out your RESTful Web Services past times sending GET as well as POST request, doing authentication, saving a cookie inward the file etc. The roll ascendance is, inward fact, the go-to tool for many Java as well as Spring developers working inward spider web application as well as consuming information from the secured RESTful Web Services, but you lot tin sack also utilisation it assay out your elementary REST Web APIs without security. I convey used it many times to depository fiscal establishment check if our Web service is upwardly or not, or is at that topographic point whatever mistake piece accessing those services.

You tin sack also utilisation this to write a rhythm out script for automatically doing health-check for your environment. All you lot demand to produce is to schedule the rhythm out script using crontab ascendance as well as it volition automatically shipping a asking as well as written report errors or success at a fixed fourth dimension of day.

Here are approximately of the useful ascendance business options of cURL command, which is extremely of import for connecting as well as testing your RESTful Web Service:

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

The syntax of roll ascendance is every bit follows:

$ roll --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@ -" pick volition instruct roll ascendance to shipping a POST asking amongst the information it reads from measure input. The '<' operator tells the rhythm out to feed a file to stdin. You tin sack acquire far simpler past times doing -d @data.xml as well as non utilisation measure input at all.

Btw, if you lot are novel to Linux, I advise you lot acquire through Learn Linux inward v Days as well as Level Up Your Career course on Udemy to acquire a head-start as well as larn approximately fundamentals + essential Linux commands you lot volition going to utilisation every day. It's an awesome course of study as well as real affordable too. I bought inward only $10 on Udemy's flash sale which happens every instantly as well as then.





Testing RESTful Web Services from Linux ascendance line 

Here is my listing of approximately of the most useful examples of roll command, which I utilisation inward my twenty-four lx minutes current to twenty-four lx minutes current life to assay out RESTful spider web services from the ascendance line. You tin sack fifty-fifty utilisation these to write scripts as well as run it from crontab to automatically assay out the availability of your RESTful API, real useful if you lot are supporting a production REST application.


1) How to assay out authentication against REST API

Suppose the URL for login inward your REST spider web service is http://localhost:8080/SpringRestDemo/j_spring_security_check hence you lot tin sack utilisation the next roll ascendance for performing login:

$ roll -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 hence hold out used past times whatever subsequent asking against the same REST Web Service.

Btw, I convey used Spring safety to protect our RESTful spider web service here, if you lot desire to larn to a greater extent than virtually that, you lot tin sack check Learn Spring Security: The Certification Class past times Eugen Paraschiv of Baeldung.   It's ane of the ameliorate course of study to larn Spring Security, which provides a guided as well as code-focused tour of Spring Security. 

 ascendance of Linux is a real powerful as well as versatile ascendance which allows you lot to shipping sophis  vii Linux cURL ascendance Examples for Testing RESTful Web Services from Command Line


2) Saving the cookie inward a file using the roll command

While authenticating against RESTful Web Service, If you lot want, you lot tin sack also relieve the cookie into a specific file using the roll ascendance 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 relieve the Cookie into /tmp/cookies.txt file.

You tin sack also relieve the cookie into your abode directory if you lot like, it volition hold out same at that topographic point every bit /tmp is by as well as large accessible to everybody as well as also it's oftentimes cleaned past times Linux.



3) Attaching header as well as Cookie into HTTP asking using Curl

You tin sack also attach HTTP headers using curl ascendance past times using option --header for authentication as well as authorisation purposes. You tin sack fifty-fifty attach cookie into your HTTP request using -b ascendance pick 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 as well as before saved cookie from /tmp/cookies.txt file into your HTTP request. The response volition await 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 amongst HTTP but currently working on a projection which has RESTful spider web service, I strongly advise you lot to showtime acquire through JSON representation of mass amongst ISBN 9783827321312, but if your REST API is secured e.g. past times using http basic auth, 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 assay out the same REST API using a browser you lot volition hold out prompted to acquire inward username as well as password past times the browser because it volition utilisation HTTP basic authentication, but roll won't produce that. You demand to particularly furnish it username as well as password every bit shown inward the adjacent example. See LDAP based authentication, every bit long every bit you lot only demand to furnish username as well as password.



6) Enabling digest authentication using the roll command

If your REST API is secured using digest authentication hence you lot tin sack utilisation --digest flag to enable HTTP digest authentication inward roll ascendance every bit well.

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

Btw, if you lot are curious virtually how to secure your API using digest authentication, well, you lot tin sack utilisation Spring security. It supports both HTTP basic as well as digest authentication. You tin sack run into Learn Spring Security for implementation details.




7) Setting to a greater extent than than ane header inward the roll command

If you lot desire you lot tin sack laid to a greater extent than than ane HTTP header inward your HTTP asking past times using -H ascendance business pick twice using roll inward UNIX, every bit shown below:

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



That's all virtually how to utilisation roll ascendance to assay out RESTful Web Services. You tin sack run these roll ascendance from Linux ascendance business window correct from the server or past times using Putty or Cygwin from your evolution machine. If you lot don't desire to utilisation the Linux ascendance line, you lot tin sack also utilisation tools similar Postman as well as approximately Chrome extension to assay out your REST API.

Further Learning
Linux Command Line Basics
answer)
  • 10 examples of lsof ascendance inward Linux? (examples)
  • 5 Books to larn RESTful Web Services  (list)
  • When to utilisation PUT or POST inward REST? (answer)
  • 10 Frequently asked RESTful Web Service Interview Questions (list)
  • How to shipping postal service amongst attachments from Linux? (mailx)
  • 10 event of Networking commands inward Unix (nslookup)
  • 5 Example of kill commands inward Unix as well as Linux (example)
  • How to uncovering all files matching specific text inward Linux (grep)
  • How to utilisation the netstat ascendance to uncovering which procedure is listening on a port? (example)
  • Linux uncovering + du + grep event (example)
  • 10 Examples of chmod ascendance inward Linux (chmod)
  • A Practical Guide to Linux Commands, Editors, as well as Shell Programming (guide)

  • Thanks for reading this article, if you lot similar this article hence delight percentage amongst your friends as well as colleagues. If you lot convey whatever proposition or feedback hence delight drib a comment.

    P. S. - If you lot are looking for approximately gratuitous online courses to start your Linux journey, you lot should depository fiscal establishment check out my listing of Free Linux Courses for Programmers, Web Developers, information technology Professionals, as well as System Administrators.

    0 Response to "Seven Linux Curlicue Ascendancy Examples For Testing Restful Spider Web Services From Ascendancy Line"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel