-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapic-gatewayscript-request.js
More file actions
71 lines (60 loc) · 2.54 KB
/
Copy pathapic-gatewayscript-request.js
File metadata and controls
71 lines (60 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @author: Rohit Goyal
*
* The GatewayScript below has been tried and test with API Connect. To run this GatewayScript, you need DataPower Gateway.
*
* The script captures commonly used code snippets to read or change incoming API request.
*/
/*
* readInputAsJSON would try to read the incoming Request as json
*
* It can take care of all HTTP methods and when Payload is empty
*
* for methods like GET, it won't return any error.
*
*/
apim.readInputAsJSON(function (error,buffer) {
if(error){
//If incoming payload is non-JSON
//throw an error
throw error;
}else{
//Read API Request
//----------------
//Read Request Headers
var requestHeaders = apim.getvariable('request.headers');
//Read Request Body
var incomingPayload = apim.getvariable('request.body');
//------------------------
//Change or Update Headers
//------------------------
apim.setvariable('message.headers.customHeader', 'sampleValue');
//------------------------------------------------------
//Read Incoming App Details using APIc Context Variables
//------------------------------------------------------
var appMetaData = {};
appMetaData.appName = apim.getvariable('client.app.name');
appMetaData.devOrgName = apim.getvariable('client.org.name');
//------------------------
//Change incoming Payload
//------------------------
var temp = {};
//buffer would be undefined when API request has Payload
temp.oldPayload = buffer;
temp.appMetaData = appMetaData;
apim.setvariable('message.body', temp);
//Setup the Content-Type for the next step
apim.output('application/json')
//-----------------------------------------
//Pass incoming Query Parameters to Backend
//------------------------------------------
//Assuming targetURL is set in Properties, read the targetURL
var targetURL = apim.getvariable('api.properties.targetURL');
//Read incoming Query String
var queryString = apim.getvariable('request.querystring');
//Set new targetURL with querystring (under invoke this would be referred as $(newTargetURL)
targetURL = targetURL + '?' + queryString;
console.error(targetURL);
apim.setvariable('newTargetURL',targetURL);
}
});