Ir al contenido principal

How to call an API through POSTMAN tool and map with X3 fields (Fuete de datos GREYTRIX)

 Fuente de datos: 

https://www.greytrix.com/blogs/sagex3/2020/07/27/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 that 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 of Request Method, Request URL, Authorization, Request Header, Request Body.

a. Request Method: 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 on URL field.

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

Fig1: Postman Authorization tab

d. Request Header: In request headers it contains 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: Body Field  contains the data, depends on type of request method, to be sent with request, raw form of data is used for sending request. 

{

“name”:”Kalam”,

“job”   :”scientist”

}

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

Fig2: Postman- Body tab screen

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 for authenticate the API

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

Fig3: Rest Web service screen

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:

Header is specified in Key-value pair, 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 URL

just declare 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 Login authentication screen, if it is successful then pass those credentials to the REST web services through code, In that case 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

1.200          -Successful request.

2.201          -Successful request and data was created.

3.204          -Empty Response.

4.400         -Bad Request.

5.401        -Unauthorized access

6.403         -Forbidden, Access denied.

7.404          -Data not found.

8.405          -Method Not Allowed or Requested method is not supported.

9.500          -Internal Server Error.

10.503       -Service Unavailable.

Response from API while executing the code above in X3.

Fig4: API Response screen in Sage 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.

Entradas populares de este blog

Valores de fstat

Fstat fstat  is a numeric status that is returned upon execution of a database operation, a sequential file operation, or a lock instruction. Syntax fstat Examples # MYTABLE is a table with a key called KEY1, that has a unique component called KEYVAL # Create a record in the table MYTABLE with they key value 1 if it doesn't exist Local File MYTABLE [MYT] Read [MYT]KEY1=1 If fstat [MYT]KEYVAL=1 : Write [MYT] If fstat MSG="The key was created in the mean time" Else MSG="Key created" Endif Else MSG="Key already exists" Endif Details fstat  is always set to '0' if the operation is successfully completed, and has a non-null value if there is an error: In a sequential read ( Getseq  and  Rdseq ),  fstat  is set to '1' at the end of the file. On  Lock ,  fstat  is set to '1' if the lock could not be performed. For a database operation ( Read ,  Look ,  Readlock ,  For ,  Write ,  ...

How to create an additional line text (ACLOB) on purchase orders for internal notes

Sin conexión Kyle Klinger hace 1 día Origen:  https://www.sagecity.com/support_communities/sage_erp_x3/f/sage-x3-general-discussion-forum/143870/how-to-create-an-additional-line-text-aclob-on-purchase-orders-for-internal-notes I was trying to add internal notes to the purchase orders at a line level and was thought I would share a how to.  If you have a better way, please share.     Add column to PORDERQ; YLINTEX2, Type TXC   Save and validate table This is where the ID of the text will be stored at the record level, i.e. POQ~00000007, this is similar to field LINTEX  On screen POH2 Add column YLINTEX2, to Block 1. Most likely you will want this field to be hidden. On column NBLIG add a button action ACLOB2, description "Text internal" This button action will require an action parameter "CODE2", it will not be available until after save. Set the parameter "CODE2" to [M:POH2]YLINTEX2(nolign-2), the field that was just added....

3 ways to send a mail from code in Sage X3, with more attachments too

Origen Fuente:  https://en.sagedev.it/sagex3/send-mail-from-code-with-attachments-sage-x3/ Autor:  https://en.sagedev.it/category/sagex3/ In this post I’ll show you how to send a mail from adonix code in Sage X3. The points we will face are: 1) Meladx/Send introduction 2) Sending  through  meladx (the best way for me: at the end with just a single code line you send a mail!) 3) Sending  through  Workflow 4) Sending  through  ENVOI_MAIL(…) From AWRKMEL Sending mail from code in Sage X3: Send vs meladx Sage X3 has two native ways for sending mails. The first way is  meladx  executable file, that you can find in  runtime\bin  directory. The seconde one is Send instruction, that was used to send mail through an application of the client station (for more information on “Send GSERMES”  go here ). The Meladx executable Meladx send messages through SMTP/POP3 protocols by means of the mail  ...