api request - SAP-archive/xsk GitHub Wiki

$.request API

Represents the client HTTP request currently being processed. This API is used for retrieving request data from the xsk HTTP service caller.

Reference

  • SAP Help

https://help.sap.com/doc/3de842783af24336b6305a3c0223a369/2.0.03/en-US/$.web.WebRequest.html

  • Module

https://github.com/SAP/xsk/blob/main/modules/api/api-xsjs/src/main/resources/META-INF/dirigible/xsk/web

  • Sample usage:
// defined a hardcoded users array for the example
let users = [
    {id: '1', name: 'John'},
    {id: '2', name: 'Ben'},
    {id: '3', name: 'George'}
]

function getUser(id) {
    // retrieve user operation by id
    return users.find(function(user) {
        return user.id === id;
    })
}

function createUser(data) {
    // create user operation which will update the users array only for the scope of the current request call as the users are hardcoded
    users.push({id: data.id, name: data.name});
}

function deleteUser(id) {
    // delete user operation which will remove a user only for the current scope of the request as the users are hardcoded
    users = users.filter(function(user) {
        return user.id !== id
    })
}

function allUsers() {
    // retrieve all users operation
    return users;
}

// get id query param 
const id = $.request.parameters.get("id");

// check the type of the request
switch($.request.method) {
    case $.net.http.PUT:
        if($.request.contentType === "application/json") {
            createUser(JSON.parse($.request.body.asString()));

            $.response.setBody(`created user [${ JSON.stringify($.request.body.asString()) }]`);
        } else {
            $.response.setBody(JSON.stringify({
                "error": "Unsupported content type."
            }));
        }
        break;
    case $.net.http.GET:
        if (id) {
            let user = getUser(id);

            $.response.setBody(JSON.stringify({ user: user }));
        } else {

            let users = allUsers();

            $.response.setBody(JSON.stringify({ users: users }));
        }

        break;
    case $.net.http.DELETE:
        if (id) {
            deleteUser(id);

            $.response.setBody(`deleted user with id [${ id }]`);
        } else {
            $.response.setBody(JSON.stringify({
                "error": `Parameter id is missing`
            }));
        }

        break;
    default:
        $.response.setBody(JSON.stringify({
            "error": `Unsupported method [${ $.request.method }]` 
        }));
}
  • Coverage
Members Description Status
body The body of the request. ✅
contentType The content type of the entity. ✅
cookies The cookies associated with the entity. ✅
entities The sub-entities of the entity. ✅
headers The headers of the entity. ✅
language Language of the request in IETF (BCP 47) format. ✅
method The HTTP method of the incoming HTTP request. ✅
parameters The parameters of the entity. ✅
path The URL path specified in the request. ✅
queryPath The URL query path specified in the request. ✅
Methods Description Status
setBody() Sets the body of the entity. ❌
  • Issues

https://github.com/SAP/xsk/issues/12

  • Unit Tests ⚠️

https://github.com/SAP/xsk/tree/main/modules/engines/engine-xsjs/src/test/resources/test/xsk/request/request.xsjs

  • Integration Tests ❌

Wiki icons legend

✅ - Feature implemented and working as supposed.
⚠️ - Feature partially implemented and needs to be finished.
❌ - Feature not implemented yet.

⚠️ **GitHub.com Fallback** ⚠️