Wrap axios to use the SAP Cloud Foundry destination Service
npm install sap-cf-axiosbash
$ npm install -s sap-cf-axios
`
Example
$3
The most simple example with a destination pointing to a service where we connect with fixed credentials.
We do not need to send the current user with the request.
`js
const SapCfAxios = require('sap-cf-axios').default;
const axios = SapCfAxios("");
axios({
method: 'POST',
url: '/BookSet',
data: {
title: "Using Axios in SAP Cloud Foundry",
author: "Joachim Van Praet"
},
headers: {
"content-type": "application/json"
}
});
`
$3
For connecting to a destination as the current user, we send the current JWT token in the authorization header of the request.
`js
const SapCfAxios = require('sap-cf-axios').default;
const axios = SapCfAxios("");
var authorization = req.headers.authorization;
const response = await axios({
method: 'GET',
url: '/iwfnd/catalogservice/',
params: {
"$format": 'json'
},
headers: {
"content-type": "application/json",
authorization
}
});
`
> NOTE: The JWT Token sent to the backend does not contain the properties name, login_name or mail. If you use principal propagation in the cloud connector you have to use ${email} or ${user_name} in the client certificate template
$3
To handle a POST request to for example CPI you can just set the name of the CSRF-Token header and the library will first do an OPTIONS call to the same URL to fetch the token and will add it to the request.
`js
const SapCfAxios = require('sap-cf-axios').default;
const cpi = SapCfAxios("cpi_destination_name");
var authorization = req.headers.authorization;
const response = await cpi({
method: 'POST',
url: '/Bookset',
headers: {
"content-type": "application/json"
},
data: {
title: "Using Axios in SAP Cloud Foundry",
author: "Joachim Van Praet"
}
xsrfHeaderName: "x-csrf-token",
data: {vatNumber},
});
`
If you want to use another method or url for fetching the X-CSRF-Token.
You can add this configuration as a third parameter to the constructor.
`js
const SapCfAxios = require('sap-cf-axios').default;
const cpi = SapCfAxios( / destination name / "cpi_destination_name", / axios default config / null, / xsrfConfig / {method: 'get', url:'/'});
var authorization = req.headers.authorization;
const response = await cpi({
method: 'POST',
url: '/Bookset',
headers: {
"content-type": "application/json"
},
data: {
title: "Using Axios in SAP Cloud Foundry",
author: "Joachim Van Praet"
}
xsrfHeaderName: "x-csrf-token",
data: {vatNumber},
});
``