cancel
Showing results for 
Search instead for 
Did you mean: 

SAP HANA XS Server - REST Web Services - How to get parameter from URL

Former Member
0 Kudos

Hi all,

we have a REST web services based on an Hana XSJS Project.

The URL of call Http POST is as follows:

http://localhost:8080/Project/prova@xxxx.it/3.50/

We have problems with reading parameters as there is the presence of the character @ in the first parameter and the point in the second.

The file .xsaccess has been populated in the following way:

{     "exposed": true,

"default_file" : "post_url.xsjs",

    "authentication": {

    "method": "Basic"    },

        "rewrite_rules" :     [

    { 

    "source": "/(\\w+)/(\\d+)/",

    "target": "/post_url.xsjs?id=$1&poiid=$2"

    }    ]

}

while the file xsjs is as follows:

$.response.contentType = "text/html";
try{
if($.request.method === $.net.http.POST) {
var id = $.request.parameters.get("id");
var poid = $.request.parameters.get("poiid");
var output = id + poid ;

$.response.setBody(output);
$.Response = $.net.http.OK;}
} catch(e){
result =  e.toString();
$.response.setBody(output + result);
}

How can I solve this issue ?

Thanks in advance,

Luigi

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Luigi,

First there are two mistakes in your .xsjs code.

1. $.Response = $.net.http.OK; -> $.response.status = $.net.http.OK;

2. result =  e.toString(); -> var result =  e.toString();

Then regarding your problem, first you need to know what the regex "/(\\w+)/(\\d+)/" means. There is a lot of learning materials, e.g. Regular expression - Wikipedia, the free encyclopedia

\w means [A-Za-z0-9_]

\d means [0-9]

So, in your case, the regex is incorrect. You need to change to the correct one. Don't know if you want to validate the email address and the number in your scenario. I do not validate anything and just use a simple one as "/(.+)/(.+)/"

The following is my code and works fine.

.xsaccess:

{     "exposed": true,

"default_file" : "post_url.xsjs",

    "authentication": {

    "method": "Basic"    },

        "rewrite_rules" :     [

    {

    "source": "/(.+)/(.+)/",

    "target": "/post_url.xsjs?id=$1&poiid=$2"

    }    ]

}

post_url.xsjs:

$.response.contentType = "text/html";

try{

if($.request.method === $.net.http.POST) {

var id = $.request.parameters.get("id");

var poid = $.request.parameters.get("poiid");

var output = id + poid ;

$.response.setBody(output);

$.response.status = $.net.http.OK;}

} catch(e){

var result =  e.toString();

$.response.setBody(output + result);

}

Best regards,

Wenjun