on 10-29-2018 7:54 PM
Hello everyone,
There is a simple method to get parameters from URL with $.request.parameters.get(name) -> so the request method has been set to GET. However, at the same time I cannot find information in the documentation how to handle POST, PUT or DELETE methods. What is more, in the documentation (XSJS - WebRequest) there is an code example which shows that there is one and only supported method => GET (code below)...
if($.request.method === $.net.http.GET) {
// get query parameter named id
var qpId = $.request.parameters.get("id");
// handle request for the given id parameter...
var result = handleRequest(qpId);
// send response
$.response.contentType = "plain/test";
$.response.setBody("result: " + result);
$.response.status = $.net.http.OK;
} else {
// unsupported method
$.response.status = $.net.http.INTERNAL_SERVER_ERROR;
}
XSJS - TupelList - only get method exists
The get method in the TupelList has nothing to do with the HTTP GET method. The get in the TupelList returns the entry of the TupelList with the name you pass as parameter to the get method. If you are executing an HTTP GET request with URL parameters (e.g. ../test.xsjs?testParam=1) then the $.request.parameters.get call returns the parameter value (e.g. $.request.parameters.get('testParam') => 1).
When you do a e.g. a POST or PUT request you pass normally your parameters as payload in the body (e.g. as JSON string). This can be accessed by $.request.body. This is a $.web.Body. With method asString you can get the body as string. If it is a JSON string, you can easily convert it to a JSON object with JSON.parse for further usage.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok, now I see! Thank you for your response. However, it's a pitty that there is no easy way to get parameters from POST or PUT and we have to parse request to JSON and then look for a parameter in loop. For me it's some kind of workaround than how it should works. What is more, it's really confusing that in documentation I've found that there is only one supported method => GET (code in my question).
Thanks again, Damian
User | Count |
---|---|
74 | |
9 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.