Protect sensitive data in custom elements
Is this page helpful?
BEARER_TOKEN
– Your Magento access tokenHOST
– demo1-m2.mage.direct
PATH
– /index.php/rest/V1/products
{Gateway API URL}
and then enter the URL in your browser address bar to try the GET request:
In our example:
If entered correctly, you will receive a JSON response from the Magento server which was delivered via your new Lambda function.
To learn more about Magento API, see more about our Magento integration.
const https = require('https');
const querystring = require('querystring');
/* ========Config Section======== */
const host = process.env.HOST;
const path = process.env.PATH;
const accessControlAllowOriginValue = process.env.ACCESS_CONTROL_ALLOW_ORIGIN;
const accessControlAllowHeadersValue = process.env.ACCESS_CONTROL_ALLOW_HEADERS;
// Bearer token authentization
const bearerToken = process.env.BEARER_TOKEN;
// Basic authentication credentials
const username = process.env.USERNAME;
const password = process.env.PASSWORD;
/* ========Config Section======== */
let authorizationHeaderValue;
if (bearerToken || (username && password)) {
authorizationHeaderValue = bearerToken ?
`Bearer ${bearerToken}` :
`Basic ${new Buffer(username + ":" + password).toString('base64')}`;
}
const request = (queryStringParameters, headers) => {
const requestOptions = {
host: host,
path: path,
port: 443,
method: 'GET',
};
if (queryStringParameters) {
requestOptions.path = `${requestOptions.path}?${querystring.stringify(queryStringParameters)}`;
}
if (authorizationHeaderValue) {
headers['Authorization'] = authorizationHeaderValue;
}
headers['Accept'] = 'application/json';
headers['accept-encoding'] = 'identity';
headers['Host'] = host;
requestOptions.headers = headers;
return new Promise((resolve, reject) => {
https.request(requestOptions, response => {
let data = '';
response.on('data', chunk => {
data += chunk;
});
response.on('end', () => {
const dataObject = JSON.parse(data);
response.data = dataObject;
resolve(response);
});
})
.on('error', error => {
reject(error);
})
.end();
});
};
exports.handler = (event, context, callback) => {
const corsHeaders = {
'Access-Control-Allow-Origin': accessControlAllowOriginValue,
'Access-Control-Allow-Headers': accessControlAllowHeadersValue
};
const repeatResponse = (response) => {
let multiValueHeaders = {};
for (const headerName in response.headers) {
if (Array.isArray(response.headers[headerName])) {
multiValueHeaders[headerName] = response.headers[headerName];
delete response.headers[headerName];
}
}
callback(null, {
statusCode: response.statusCode,
body: JSON.stringify(response.data),
headers: { ...response.headers, ...corsHeaders },
multiValueHeaders: multiValueHeaders,
});
};
const sendError = (error) => {
callback(null, {
statusCode: '400',
body: JSON.stringify(error),
headers: corsHeaders,
});
};
switch (event.httpMethod) {
case 'GET':
request(event.queryStringParameters, event.headers)
.then((response) => {
repeatResponse(response);
})
.catch(error => {
sendError(error);
});
break;
default:
sendError(new Error(`Unsupported method "${event.httpMethod}"`));
}
};
{Gateway API URL}?searchCriteria[pageSize]=10&searchCriteria[filterGroups][0][filters][0][field]=name&searchCriteria[filterGroups][0][filters][0][conditionType]=like&searchCriteria[filterGroups][0][filters][0][value]=%25watch%25
https://vpzvj1fspi.execute-api.eu-central-1.amazonaws.com/default/requestRepeater?searchCriteria[pageSize]=10&searchCriteria[filterGroups][0][filters][0][field]=name&searchCriteria[filterGroups][0][filters][0][conditionType]=like&searchCriteria[filterGroups][0][filters][0]
headers['Accept'] = 'application/xml';
.