Skip to content

How to do a POST request to a REST Endpoint

A POST request is send to a REST endpoint to get a response based on the data sent as the request payload. The following example highlights how to send a POST request to the IFS AI Server to get a machine learning prediction response.


CREATE OR REPLACE PACKAGE BODY PREDICTION_REST_CALL AS   

PROCEDURE  prediction_rest_postIS  
Request PLSQLAP_DOCUMENT_API.Document;  
url_params_ PLSQLAP_DOCUMENT_API.Document;  

BEGIN  
Request := PLSQLAP_DOCUMENT_API.New_Document('PREDICTION_REQUEST');  
PLSQLAP_DOCUMENT_API.Add_Attribute(Request,'cust_id', '10001');  
PLSQLAP_DOCUMENT_API.Add_Attribute(Request,'payment_amt', '200.00');  
PLSQLAP_DOCUMENT_API.Add_Attribute(Request,'currency', 'USD');  
......  
PLSQLAP_DOCUMENT_API.Add_Attribute(Request,'status', 'active');  

url_params_ := PLSQLAP_DOCUMENT_API.New_Document('URL_Parameters');  
PLSQLAP_DOCUMENT_API.Add_Attribute(url_params_, 'param1', 'invoice_prediction');  

plsql_rest_sender_API.Call_Rest_EndPoint(rest_service_ => 'PREDICTION',  
                                         message_payload_ => Request,  
                                         url_params_ => url_params_,  
                                         callback_func_ => 'Invoice_Pred_API.REST_callback',  
                                         http_req_headers_ => 'Content-type: application/json',  
                                         http_method_ => 'POST',  
                                         key_ref_ => 'COMPANY=10^INVOICE_ID=1000123^',  
                                         fnd_user_ => 'ALAIN');  

END prediction_rest_post;  

END PREDICTION_REST_CALL;  

Important points to consider:

  • When configuring the REST service as a routing address we only configure the root URL of the REST service. To construct the exact path of the resource that we wish to send the POST request to we have to create a record structure in the PL/SQL code. Then set the values for the place holders that is specified in the REST Routing address in that record structure. In the code sample above the variable url_params_ is used for this purpose.
  • The payload that is required for the POST request is constructed using a record strucure. In the above example the record structure Record is used for this purpose.
  • It is also possible to send a CLOB as the payload. This CLOB can contain an XML structure which has the payload expected by the REST service. you have to use the following method to send the CLOB

plsql_rest_sender_API.Call_Rest_EndPoint (  
rest_service_           IN     VARCHAR2,  
xml_                    IN     CLOB,  
url_params_             IN     type_record_ DEFAULT NULL,  
callback_func_          IN     VARCHAR2,  
http_method_            IN     VARCHAR2,  
http_req_headers_       IN     VARCHAR2 DEFAULT NULL,  
query_parameters_       IN     type_record_ DEFAULT NULL,  
header_params_          IN     type_record_ DEFAULT NULL,  
fnd_user_               IN     VARCHAR2 DEFAULT NULL,  
key_ref_                IN     VARCHAR2 DEFAULT NULL,  
sender_                 IN     VARCHAR2 DEFAULT NULL,  
receiver_               IN     VARCHAR2 DEFAULT default_recevier_,  
message_type_           IN     VARCHAR2 DEFAULT 'EVENT',  
subject_                IN     VARCHAR2 DEFAULT NULL,  
in_order_               IN     BOOLEAN  DEFAULT FALSE)  

  • The type of the payload is specified using the parameter http_req_headers_ by setting the Content-type header. Refer to following documentation to get a better idea on how to specify different request headers  >>.