Friday, September 11, 2020

Curl command - Test a rest API

About curl command:

  • curl is a command line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP or FILE).
  •  curl is powered by Libcurl. This tool is preferred for automation, since it is designed to work without user interaction. curl can transfer multiple file at once

syntax:

curl [options] [URL...]

Here are the options that we’ll use when making requests:

-X, --request - The HTTP method to be used.

-i, --include - Include the response headers.

-d, --data - The data to be sent.

-H, --header - Additional header to be sent.


Verbose:

When we are testing, it’s a good idea to set the verbose mode on:

curl -v http://www.example.com/

As a result, the commands would provide helpful information such as the resolved IP address, the port we are trying to connect to and the headers.

Example:

curl  -v -d '{"Customer": {"ID": "ID10","Name": "Name11","Address": "Address12"}}' -H 'Content-Type: application/json' http://localhost:8001/soa-infra/resources/POC/LoopInBPELProject/RestService/abc

* Connected to host  (host ip) port 8001

> POST /soa-infra/resources/POC/LoopInBPELProject/RestService/abc HTTP/1.1

User-Agent: curl/7.9.3 (powerpc-ibm-aix5.3.0.0) libcurl 7.9.3 (OpenSSL 0.9.8y) (ipv6 enabled)

Host: localhost:8001

Pragma: no-cache

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*

Content-Type: application/json

Content-Length: 68

{"Customer": {"ID": "ID10","Name": "Name11","Address": "Address12"}}

<CustomersRes xmlns="http://www.cust.abc">

   <Outcome>ID10</Outcome>

</CustomersRes>

* Connection #0 left intact

* Closing connection #0

Output:

By default, curl outputs the response body to standard output. Optionally, we can provide the output option to save to a file:

curl -o out.json http://www.example.com/index.html

This is especially helpful when the response size is large.

Example:

curl  -o response.xml -d '{"Customer": {"ID": "ID10","Name": "Name11","Address": "Address12"}}' -H 'Content-Type: application/json' http://host:8001/soa-infra/resources/POC/LoopInBPELProject/RestService/abc
-rw-r--r--    1 oracle   dba              86 Sep 11 06:20 response.xml
cat response.xml
<CustomersRes xmlns="http://www.cust.abc">
   <Outcome>ID10</Outcome>
</CustomersRes>

POST:
We use this method to send data to a receiving service. And for that, we use the data option.

curl -d 'id=9&name=sri' http://localhost:8082/testurl
or, pass a file containing the request body to the data option like this:

curl -d @request.json -H "Content-Type: application/json" 
  http://localhost:8082/testurl

Example:
curl   -d @request.json -H 'Content-Type: application/json' http://host:8001/soa-infra/resources/POC/LoopInBPELProject/RestService/abc
<CustomersRes xmlns="http://www.cust.abc">
   <Outcome>ID10</Outcome>
</CustomersRes>

GET:
This is the default method when making HTTP calls with curl. In fact, the examples previously shown were plain GET calls.
While running a local instance of a service at port 8082, we'd use something like this command to make a GET call:
curl -v http://localhost:8082/testurl

Authentication:
If the API endpoint requires authentication, you’ll need to obtain an access key. Otherwise, the API server will respond with the “Access Forbidden” or “Unauthorized” response message.

The process of obtaining an access key depends on the API you’re using. Once you have your access token you can send it in the header:

curl -X GET -H "Authorization: Bearer {ACCESS_TOKEN}" "https://api.server.io/posts"

-u  option: curl also provides options to download files from user authenticated FTP servers.
Syntax:

curl -u {username}:{password} [FTP_URL]


how to store the rest response to a variable using shell script:
cat > curl.sh
#!/usr/bin/bash
var=$(curl -d '{"Customer": {"ID": "ID10","Name": "Name11","Address": "Address12"}}' -H 'Content-Type: application/json' http://uadcc-elmweb01:8001/soa-infra/resources/POC/LoopInBPELProject/RestService/abc)
echo $var

chmod 777 curl.sh
./curl.sh
  % Total    % Received % Xferd  Average Speed          Time             Curr.
                                 Dload  Upload Total    Current  Left    Speed
100    86  100    86    0     0   1954      0  0:00:00  0:00:00  0:00:00 86000
<CustomersRes xmlns="http://www.cust.abc"> <Outcome>ID10</Outcome> </CustomersRes>

No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...