SapHanaTutorial.Com HOME     Learning-Materials Interview-Q&A Certifications Quiz Online-Courses Forum Jobs Trendz FAQs  
     Explore The World of Hana With Us     
About Us
Contact Us
 Apps
X
HANA App
>>>
Hadoop App
>>>
Tutorial App on SAP HANA
This app is an All-In-One package to provide everything to HANA Lovers.

It contains
1. Courses on SAP HANA - Basics, Modeling and Administration
2. Multiple Quizzes on Overview, Modelling, Architeture, and Administration
3. Most popular articles on SAP HANA
4. Series of Interview questions to brushup your HANA skills
Tutorial App on Hadoop
This app is an All-In-One package to provide everything to Hadoop Lovers.

It contains
1. Courses on Hadoop - Basics and Advanced
2. Multiple Quizzes on Basics, MapReduce and HDFS
3. Most popular articles on Hadoop
4. Series of Interview questions to brushup your skills
Apps
HANA App
Hadoop App
';
Search
Stay Connected
Search Topics
Topic Index
+
-
XSA Overview
+
-
XS Overview
+
-
Hello World Example
+
-
XS OData Service
+
-
HANA XSJS Service
+
-
Core Data Service
+
-
Others

XSJS - HTTP Request Handling

In this article we will show you an XSJS example to demonstrate – How to parse http request inside XSJS.
Note: To check all the examples on XSJS, read the article SAP HANA XSJS Examples

Introduction:

HTTP request contains many metadata information like header details, method type, query parameters, host and port no etc.
We can use XSJS $.request API to get these information if required.
For example:
If the URL is
http://hana_host:8000/mypackage/xsjs_service.xsjs?param1=value1&param2=value2
Then,
$.request.parameters get(‘param1’) will return value1
$.request.headers.get('host') will return hana_host

Now, let us see a detail example of $.request API.

Example:

Create an XSJS file and paste the below code.

function performAdd() {
    var body = '';
    var num1 = $.request.parameters.get('num1');
    var num2 = $.request.parameters.get('num2');
   
    var answer;

    //request.parameters are of type String. Parse them to Integer
    answer = parseInt(num1) + parseInt(num2);

    body = answer.toString();

    $.response.setBody(body);
    $.response.status = $.net.http.OK;
}

function getRequestDetails() {
       var queryParameterDetails = 'QUERY PARAMETER DETAILS:' +" <br><br>";
       var noOfParams = $.request.parameters.length;
       queryParameterDetails = queryParameterDetails + 'NO OF QUERY PARAMTERS ARE:' + noOfParams + " <br>";
       queryParameterDetails = queryParameterDetails + 'QUERY PARAMTERS AND THEIR VALUES ARE: ' + " <br>";
      
       var i;
       for (i = 0; i < $.request.parameters.length; ++i) {
              queryParameterDetails = queryParameterDetails + $.request.parameters[i].name + ': ' + $.request.parameters[i].value + '<br>';
       }
      
       var requestHeaderDetails = '<br>' + 'REQUEST HEADER DETAILS:' + '<br><br>';
       requestHeaderDetails = requestHeaderDetails + 'HOST NAME IS: ' + $.request.headers.get('host')+ " <br>";
       requestHeaderDetails = requestHeaderDetails + 'REQUEST METHOD IS: ' + $.request.headers.get('~request_method')+ " <br>";
       requestHeaderDetails = requestHeaderDetails + 'SERVER PORT IS: ' + $.request.headers.get('~server_port')+ " <br>";
       requestHeaderDetails = requestHeaderDetails + 'CLIENT PROTOCOL IS: ' + $.request.headers.get('clientprotocol')+ "<br>";
       requestHeaderDetails = requestHeaderDetails + 'URL PATH: ' + $.request.headers.get('~path')+ " <br>";
       requestHeaderDetails = requestHeaderDetails + 'QUERY STRING: ' + $.request.headers.get('~query_string')+ " <br><br>";
      
      
       var httpMethodName = 'HTTP METHOD IS: ';
       switch ($.request.method) {
       case $.net.http.GET:
              httpMethodName = httpMethodName + 'GET';
              break;
       case $.net.http.POST:
              httpMethodName = httpMethodName + 'POST';
              break;
       case $.net.http.PUT:
              httpMethodName = httpMethodName + 'PUT';
              break;
       default:
              break;
       }     
       $.response.contentType = "text/html";
       $.response.status = $.net.http.OK;
       $.response.setBody(queryParameterDetails + requestHeaderDetails + httpMethodName);

}

var aCmd = $.request.parameters.get('cmd');
switch (aCmd) {
    case "add":
        performAdd();
        break;
    case "metadata":
        getRequestDetails();
        break;
    default:
       $.response.status = $.net.http.BAD_REQUEST;
       $.response.setBody('Invalid Command');
      
}

Run XSJS Service:

Run the XSJS service and pass the query parameters as:
http://hana_host:8000/mypackage/HttpRequestHandling.xsjs?cmd=add&num1=20&num2=30
The output will give the addition of 2 number as below:
SAP HANA XSJS HTTP Request

Change the query parameter as
http://hana_host:8000/mypackage/HttpRequestHandling.xsjs?cmd=metadata&num1=20&num2=30
The output will give Http request details as below:
SAP HANA XSJS HTTP Request

Download Full Source Code:

Click here to download the full source code. Follow the steps mentioned in “How to Run.txt” to run it.

Reference:

SAP HANA XS JavaScript Reference

Continue reading:




Support us by sharing this article.



Explore More
Close X
Close X

4 thoughts on “SAP HANA XSJS – HTTP Request Handling

  1. Lajvanth sardar says:

    Hi the blog is beautiful and easy to understand….

    But Reference link is not working : Can you enable with correct hyperlink

    SAP HANA XS JavaScript Reference

    Thank you,,

    • Admin says:

      Hello,

      Thanks for noticing, SAP links get changed often, thank for pointing it out.
      We have replaced it with new url.

      Thanks,
      Admin

  2. Meenal says:

    I find all the articles are very useful. Please keep up the good work!
    They are easy to understand as explanations are given with great clarity.

Leave a Reply

Your email address will not be published. Required fields are marked *

Current day month ye@r *

 © 2017 : saphanatutorial.com, All rights reserved.  Privacy Policy