Hi Stefano,
A great place to look in the documentation for this is https://www.ibm.com/docs/en/api-connect/10.0.1.x?topic=aplc-using-context-variables-in-gatewayscript-xslt-policies-datapower-api-gateway which has examples for both of these.
For getting a request header
var auth = apim.getvariable('request.headers.authorization').toString();
use
var auth = context.request.header.get('authorization').toString();
the toString() function is redundant as the header is already a string. Note that the header.get function is case insensitive. You could also use a context.get('request.headers.authorization')
function but that is case sensitive, so it would be best to use the header.get function instead.
For
apim.error('authentication_error', 401, 'Unauthorized', error);
The apim.error v5(c) function allowed you to specify the error name, HTTP status code and reason phrase, and an error string, and would reject the transaction by under the covers doing a session.reject
function. The context.reject
function will abort your GatewayScript action, specifying the error name and error string, but setting the HTTP status code and phrase is done by setting message.statusCode explicitly.
context.reject('authentication_error', error);
context.message.statusCode = '401 Unauthorized';
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
------------------------------
Original Message:
Sent: Mon October 09, 2023 08:39 AM
From: Stefano Paci
Subject: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis
Hi Steve,
Thank you for explaining this, I need a native way to retrieve this header
var auth = apim.getvariable('request.headers.authorization').toString();
And for returning an authentication error
apim.error('authentication_error', 401, 'Unauthorized', error);
what's the best practice here?
------------------------------
Stefano Paci
Original Message:
Sent: Mon September 18, 2023 09:45 AM
From: Steve Linn
Subject: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis
Hi Sergey,
The native way to retrieve the swagger in a v10 API Gateway API without using the apim compatibility module is to use the asynchronous readAsJSON function as follows
context.swagger.readAsJSON(function(error, swaggerDoc) { // continue with the code that would have been done after the apim.getSwaggerDocument // with either by embedding that code here or calling a callback function};
The apim.getSwaggerDocument compatibility function will call the callback function for you, but you'll need to make that call yourself.
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Thu September 14, 2023 04:02 AM
From: Sergey Paponov
Subject: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis
Hi Steve,
Thank for your explanation.
But if I need to get full document (full API's swagger/yaml) that has possible to retrieve in version v5 and v10.0.1.9 without warning by methods: apim.getVariable('document') in v5 and apim.getSwaggerDocument() in v10.0.1.9 , how can I do this?
Maybe there is any workaround ?
Thanks,
And with Regards,
Sergey
------------------------------
Sergey Paponov
Original Message:
Sent: Wed October 26, 2022 04:24 PM
From: Steve Linn
Subject: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis
Hi Hamza,
The apim module in v10 provides v5 compatibility. The module also interfaces the new v10 context with the migrated code using the apim.functionname without needed to change. The recommendation for new APIs is to use the native context functions (and thus the warnings), but understand that the results of the context functions may require some additional or different coding procedures. For example, apim.getvariable('contextname.body') if that payload is an XML payload as determined by contextname.headers.content-type, in v5 was returned as a XML Nodelist, whereas a parsed XML document in the v10 context will return a XML Document. The apim module handles that difference for you assuming you have subsequent nodelist functions processing that nodelist, whereas when writing new code, you work directly with the XML Document that is returned.
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Tue October 25, 2022 10:34 AM
From: Hamza Bayar
Subject: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis
I use apim and I add var apim = require('apim') to get and set variables from header and body. it's good with the apim but i have this warning .. How i can solve it ?
------------------------------
Hamza Bayar
------------------------------