Thursday, October 31, 2019

Vii Linux Coil Ascendency Examples For Testing Restful Spider Web Services From Ascendency Line

The curl or cURL dominance of Linux is a real powerful too versatile dominance which allows you lot to transportation sophisticated HTTP asking correct from your Linux dominance trouble window. You tin work the cURL dominance to attempt out your RESTful Web Services past times sending GET too POST request, doing authentication, saving a cookie inwards the file etc. The ringlet dominance is, inwards fact, the go-to tool for many Java too Spring developers working inwards spider web application too consuming information from the secured RESTful Web Services, but you lot tin also work it attempt out your uncomplicated REST Web APIs without security. I convey used it many times to cheque if our Web service is upward or not, or is at that spot whatsoever mistake piece accessing those services.

You tin also work this to write a musical rhythm out script for automatically doing health-check for your environment. All you lot demand to produce is to schedule the musical rhythm out script using crontab dominance too it volition automatically transportation a asking too written report errors or success at a fixed fourth dimension of day.

Here are approximately of the useful dominance trouble options of cURL command, which is extremely of import for connecting too testing your RESTful Web Service:

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

The syntax of ringlet dominance is every bit follows:

$ ringlet --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@ -" selection volition instruct ringlet dominance to transportation a POST asking alongside the information it reads from measure input. The '<' operator tells the musical rhythm out to feed a file to stdin. You tin acquire inwards simpler past times doing -d @data.xml too non work measure input at all.

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





Testing RESTful Web Services from Linux dominance line 

Here is my listing of approximately of the most useful examples of ringlet command, which I work inwards my 24-hour interval to 24-hour interval life to attempt out RESTful spider web services from the dominance line. You tin fifty-fifty work these to write scripts too run it from crontab to automatically attempt out the availability of your RESTful API, real useful if you lot are supporting a production REST application.


1) How to attempt out authentication against REST API

Suppose the URL for login inwards your REST spider web service is http://localhost:8080/SpringRestDemo/j_spring_security_check thence you lot tin work the next ringlet dominance for performing login:

$ ringlet -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 thence survive used past times whatsoever 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 nearly that, you lot tin check Learn Spring Security: The Certification Class past times Eugen Paraschiv of Baeldung.   It's 1 of the amend course of study to larn Spring Security, which provides a guided too code-focused tour of Spring Security. 

 dominance of Linux is a real powerful too versatile dominance which allows you lot to transportation sophis  vii Linux cURL dominance Examples for Testing RESTful Web Services from Command Line


2) Saving the cookie inwards a file using the ringlet command

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

You tin also salve the cookie into your abode directory if you lot like, it volition survive same at that spot every bit /tmp is by too large accessible to everybody too also it's oftentimes cleaned past times Linux.



3) Attaching header too Cookie into HTTP asking using Curl

You tin also attach HTTP headers using curl dominance past times using option --header for authentication too authorisation purposes. You tin fifty-fifty attach cookie into your HTTP request using -b dominance selection 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 too 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 alongside HTTP but currently working on a projection which has RESTful spider web service, I strongly advise you lot to get-go acquire through JSON representation of mass alongside ISBN 9783827321312, but if your REST API is secured e.g. past times using http basic auth, thence 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 attempt out the same REST API using a browser you lot volition survive prompted to move inwards username too password past times the browser because it volition work HTTP basic authentication, but ringlet won't produce that. You demand to particularly furnish it username too password every bit shown inwards the side past times side example. See LDAP based authentication, every bit long every bit you lot simply demand to furnish username too password.



6) Enabling digest authentication using the ringlet command

If your REST API is secured using digest authentication thence you lot tin work --digest flag to enable HTTP digest authentication inwards ringlet dominance every bit well.

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

Btw, if you lot are curious nearly how to secure your API using digest authentication, well, you lot tin work Spring security. It supports both HTTP basic too digest authentication. You tin run across Learn Spring Security for implementation details.




7) Setting to a greater extent than than 1 header inwards the ringlet command

If you lot desire you lot tin laid to a greater extent than than 1 HTTP header inwards your HTTP asking past times using -H dominance trouble selection twice using ringlet inwards UNIX, every bit shown below:

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



That's all nearly how to work ringlet dominance to attempt out RESTful Web Services. You tin run these ringlet dominance from Linux dominance trouble window correct from the server or past times using Putty or Cygwin from your evolution machine. If you lot don't desire to work the Linux dominance line, you lot tin also work tools similar Postman too approximately Chrome extension to attempt out your REST API.

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

  • Thanks for reading this article, if you lot similar this article thence delight portion alongside your friends too colleagues. If you lot convey whatsoever proposition or feedback thence delight drib a comment.

    P. S. - If you lot are looking for approximately costless online courses to start your Linux journey, you lot should cheque out my listing of Free Linux Courses for Programmers, Web Developers, information technology Professionals, too System Administrators.

    No comments:

    Post a Comment