Monday, 2 May 2022

HTTP Services in SAP BTP

Definition:

The HTTP service is the component of the Application Server that provides facilities for deploying web applications and for making deployed web applications accessible by HTTP clients.

We have seen HTTP and HTTPS services in SAP ERP, there are many steps involved to create and enable this service in ERP. But in BTP its relatively Simpler, Let’s look at the steps for creating HTTP service in SAP BTP.

Creation:

Right click on the Respective package -> New -> Other Repository Objects

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

From the menu choose Connectivity -> HTTP Service

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

Next -> fill out Name, Description, Handler class

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

Next -> Attach it to a TR -> Finish

Your HTTP service Is ready, and it should look Something like :

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

It will have name, handler class name and URL. if you copy paste the URL to your browser then the you will be asked to logon and a blank HTML page will be shown, since we haven’t written any code.

NB: The class will be automatically created, if it is not existing and it will have few lines of code

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

Now I will make a minor change to the code,

class ZCL_HTTP_SERV definition
  public
  create public.
public section.
  interfaces IF_HTTP_SERVICE_EXTENSION.
protected section.
private section.
ENDCLASS.
CLASS ZCL_HTTP_SERV IMPLEMENTATION.
  method IF_HTTP_SERVICE_EXTENSION~HANDLE_REQUEST.
  response->set_text( 'Hi Bloggers, The HTTP service is Up and Running' ).
  endmethod.
ENDCLASS.

If you reload the HTML page You should see the page as :

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

The class should use the Interface: IF_HTTP_SERVICE_EXTENSION

The interface has only one method: HANDLE_REQUEST

The Method has two parameters:

request - Incoming request object for an HTTP Service.

response - Outgoing response object for an HTTP Service

Request has the following methods:

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

Each method serves the function as the name itself suggests.

Response has the following methods:

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

Now I will make changes to the class to handle the Get and Post requests send by the browser via HTTP service using the methods mentioned above.

CLASS zcl_http_serv DEFINITION
  PUBLIC
  CREATE PUBLIC.

  PUBLIC SECTION.

    INTERFACES if_http_service_extension. "Standard Interface
  PROTECTED SECTION.
  PRIVATE SECTION.
    METHODS: get_html RETURNING VALUE(html) TYPE string. "GET Request HTML Generator Method
    METHODS: post_html                                   "POST Request HTML Generator Method
      IMPORTING
                First_Name  TYPE string
                Last_Name   TYPE string
                Designation TYPE string
                Company     TYPE string
                Phone_Num   TYPE string
      RETURNING VALUE(html) TYPE string.

    CLASS-DATA url TYPE string.

ENDCLASS.

CLASS zcl_http_serv IMPLEMENTATION.

  METHOD if_http_service_extension~handle_request. "Standard Method Which Handles the HTTP request
    url = 'https://45f514af-f57b-4579-990b-8b70ea328491.abap-web.us10.hana.ondemand.com:443/sap/bc/http/sap/z_http_serv/?sap-client=100'. " URL For the HTTP service #Generated while creation
    CASE request->get_method( ). "Getting the request's HTTP method ie GET, POST, PUT, PATCH, DELETE Etc.

      WHEN CONV string( if_web_http_client=>get ). "if its a GET Request

        response->set_text( get_html( ) ).         " Sets the response text as an HTML file, so that the browser could show a web page as response.

      WHEN CONV string( if_web_http_client=>post ). "if its a Post Request

        DATA(First_Name) = request->get_form_field(  `fname` ). "gets the parameters values Sent by POST request Using the  parameter name
        DATA(Last_Name) = request->get_form_field(  `lname` ).
        DATA(Designation) = request->get_form_field(  `Designation` ).
        DATA(Company) = request->get_form_field(  `Company` ).
        DATA(Phone_Num) = request->get_form_field(  `phno` ).

        response->set_text( post_html(
                                      EXPORTING First_Name  = First_Name "Sets the response text as an HTML file, so that the browser could show a web page as response.
                                                Last_Name   = Last_Name
                                                Designation = Designation
                                                Company     = Company
                                                Phone_Num   = Phone_Num
                                        ) ).
    ENDCASE.

  ENDMETHOD.

  METHOD get_html.    "Response HTML for GET request

    html = |<html> \n| &&
  |<body> \n| &&
  |<title>Registration Form </title> \n| &&            " The HTML page has few input fields and a submit button while clicking the submit button the page sends the
  |<form action="{ url }" method="POST">\n| &&         " values entered in the input fields as a POST request.
  |<H2>Blogger Registration</H2> \n| &&
  |<label for="fname">First name: </label> \n| &&
  |<input type="text" id="fname" name="fname" required ><br><br> \n| &&
  |<label for="lname">Last name:</label> | &&
  |<input type="text" id="lname" name="lname" required ><br><br> \n | &&
  |<label for="Designation">Designation:</label> | &&
  |<input type="text" id="Designation" name="Designation" required ><br><br> \n | &&
  |<label for="Company">Company:</label> | &&
  |<input type="text" id="Company" name="Company" required ><br><br> \n | &&
  |<label for="phno">Phone Number:</label> | &&
  |<input type="number" id="phno" name="phno" required ><br><br> \n | &&
  |<input type="submit" value="Submit"> \n| &&
  |</form> | &&
  |</body> \n| &&
  |</html> |.

  ENDMETHOD.

  METHOD post_html. "Response HTML for POST request

    html = |<html> \n| &&
   |<body> \n| &&
   |<title>Registration Form </title> \n| && " HTML page is the response page after the submit happens from First page
   |<form action="{ url }" method="Get">\n| &&
   |<H2>Blogger Registration Success </H2> \n| &&
   |<p> Thanks { First_Name  } { Last_Name } working for { Company } company as { Designation }, </p> | &&
   |<p>  for Registering with knowledge sharing BLOGS Platform. We will contact you on { Phone_Num } for further process.  </p> | &&
   |<input type="submit" value="Go Back"> \n| &&
   |</form> | &&
   |</body> \n| &&
   |</html> |.
  ENDMETHOD.
ENDCLASS.

When we first loads the URL it will automatically call a GET request so the first HTML will be sent as a response and the page would look like :

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

After clicking the submit button our page sends a POST request to our HTTP service along with the data we entered and the POST response html will be displayed.

SAP ABAP Development, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Exam, SAP ABAP Preparation

Other than using the ODATA services in cloud, we could use the HTTP services from now on. So that the limitations of ODATA could be avoided. I would like to look at the BTP platform as an extension, so in most of the cases creating and using an entire ODATA service with the much more heavy Behavior Implementations are a burden. Even though ODATA services are great, sometimes we might not need them or in some scenarios ODATA services just does not suite.

The example code used in the blog is just for imitating how the HTTP service implementation would be called. Most of the SAP standard functions used in the earlier versions would also work here.

No comments:

Post a Comment