How to Call an API Through POSTMAN Tool and Map With X3 Fields

By | July 27, 2020

We come across the scenario, where we will use POSTMAN software tool to examine the working of external API. Postman is a simple GUI for sending HTTP requests and viewing responses. REST services are available in Sage x3 which returns data in a JSON format. In this blog, we are going to visualize the mapping of fields from Postman to x3 Rest web services.

Example: In Postman IDE,

1. HTTP Request — Request is the simplest way possible to make HTTP calls.

HTTP Request contains the Request Method, Request URL, Authorization, Request Header, and Request Body.

a. Request Method: The request method defines the type of request to be made. There are mainly four request methods, used for creating/updating, retrieving, and deleting data.

  • POST Request — For Creating Or Updating data
  • PUT Request — For Updating data
  • GET Request For Retrieving/Fetching data
  • DELETE Request For Deleting data.

Choose Post Method, A POST request is a method that is used when we need to send some additional information inside the body of the request to the server.

b. Request URL: URL to make the HTTP request. Enter the complete URL in the URL field.

c. Authorization: An authorization token, included with requests, is used to identify the requester. Select the Basic Auth on the Type field and enter a valid Username and Password.

d. Request Header: In request headers, it contains the key-value of the application.

Content-Type – A content type describes the format of object data. Content-type, i.e., application/JSON which is used for the requests and responses

e. Request Body: The body Field contains the data, depending on the type of request method, to be sent with the request, the raw form of data is used for sending the request. 

{

“name”:”Kalam”,

“job”   :”scientist”

}

2. HTTP Response — On click of the Send Button, the Response will display in JSON Format.

In X3:Create REST Web services

Navigational Path: All->Administration->Administration->Rest Web services

Name Field: Enter any user defined field

Base URL: Enter the domain name of the URL

Example: Enter only domain name: http://reqres.in  from full path:   http://reqres.in/api/users       

Content-Type: Choose Json

Authentication: Choose Basic Authentication and Enter credentials, Username and password used to authenticate the API

Parameters: PARAM tab in Postman is mapped with Parameters fields in X3 as Key-value Pair

In X3:Below Code Snippet to call external API

  ##Declaration of Variables used in executing RestWebservice##

     LOCAL CHAR YAPI(250),YURI(250),PCOD(100)(1..100),

     PVAL(100)(1..100),HCOD(100)(1..100),HVAL(100)(1..100)

     LOCAL CLBFILE YMTD,YREQBODY,YRESBODY,RESHEAD(0)

     LOCAL CLBFILE RESBODY

     LOCAL INTEGER RETVAL

     ##Declaring Header ,Method and Path of URL through Code

     HCOD(1)   =  “Authorization”

     HVAL(1)    =  ‘”X3User:UserX3″‘

     HCOD(2)   =  “Content-Type”

     HVAL(2)    =  ‘”application/json”‘

     YMTD        =  ‘POST’

     YAPI         =  ‘CreateID’

     YURI         =  ‘/api/users’

     YRESBODY =  ”

     YREQBODY =  ‘{“name”: “Kalam”,”job”: “scientist”}’

     ##Call EXEC_REST_WS Method from ASYRRESTCLI library function

     RETVAL  = Func ASYRRESTCLI.EXEC_REST_WS(YAPI,YMTD,YURI,PCOD,PVAL,HCOD,HVAL,YREQBODY,0,”,RESHEAD,YRESBODY)

     IF(RETVAL=201)

        INFBOX NUM$(YRESBODY)

     ELSIF(RETVAL=500)

        INFBOX NUM$(“CONNECTIVITY ISSUE!!!”)  

     ENDIF

Explanation of Code snippet:

The header is specified in the Key-value pair, the Header should contain authorization and content-type details.

Assign the variable HCOD(1)- Header code of array index 1 to Authorization.

Assign the variable HVAL(1) -Header variable of array index 1 to username and password separated with Colon.

Assign the variable HCOD(2)- Header code of array index 2 to Content-type.

Assign the variable HVAL(2)- Header variable of array index 2 to application/Json.

Assign the variable YMTD-Type of Method used  to string value “POST”

Assign the variable YAPI -REST web service Name to ” CreateID “

Assign the variable YURI with the remaining part of the URL

just declare the Response body variable

and Assign the YREQBODY variable to input

call EXEC_REST_WS function from ASYRRESTCLI library with all required parameters and will return status code as integer value which shows success or failure and result json format is stored in YRESBODY Variable.

IMPORTANT NOTE:

If there is an requirement of sending Username and password directly through code without passing in REST webservices. Explanation with scenario,

From the Login authentication screen, if it is successful then pass those credentials to the REST web services through code, In that case, the Header variable should contain

Header Code HCOD(1) as “Authorization” and

Header Val HVAL(1) as ” Basic MTcwZWU5MmEyODOTo= “

String “Basic” followed by BASE 64 format of Username: Password

Refer the blog for the ASYRRESTCLI function and its parameters to be passed for the function

“The narration of function and its parameter used to call an external/outgoing REST web service from ASYRRESTCLI library”

Few List of Status Code and its description

  • 200 – Successful request.
  • 201 – Successful requests and data were created.
  • 204 – Empty Response.
  • 400 – Bad Request.
  • 401 – Unauthorized access
  • 403 – Forbidden, Access denied.
  • 404 – Data not found.
  • 405 – Method Not Allowed or Requested method is not supported.
  • 500 – Internal Server Error.
  • 503 – Service Unavailable.

Response from API while executing the code above in X3.

This blog helps us to understand the mapping of fields from POSTMAN tool to X3 REST web services and code used to call an external API by using EXEC_REST_ES function by passing credentials in header variables.