How to get all Apigee request headers in JavaScript? - Stack Overflow

I have some JavaScript in which I need to work with all the request headers in Apigee. The online doc (

I have some JavaScript in which I need to work with all the request headers in Apigee. The online doc ( ) mentions them available via context.proxyRequest.headers as name/value pairs. Do I have to iterate through them, or are they available en masse as an array via context.proxyRequest.headers?

Thanks.

I have some JavaScript in which I need to work with all the request headers in Apigee. The online doc ( http://apigee./docs/api-services/content/javascript-object-model ) mentions them available via context.proxyRequest.headers as name/value pairs. Do I have to iterate through them, or are they available en masse as an array via context.proxyRequest.headers?

Thanks.

Share Improve this question asked Mar 24, 2014 at 22:46 Kerb IonKerb Ion 311 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

This code works for me to examine all the request headers:

// reflectHeaders.js
// ------------------------------------------------------------------
//
// formats the request headers into a JSON response.
//
// created: Tue May 13 11:42:19 2014
// last saved: <2014-May-13 12:05:39>

var requestHeaders = context.getVariable("request.headers.names"),
    result = {};

// requestHeaders is a java.util.TreeMap$KeySet; convert it to string
requestHeaders = requestHeaders + '';

// convert from "[A, B, C]" to an array of strings: ["A", "B", "C"]
requestHeaders = requestHeaders.slice(1, -1).split(', ');

// insert each header into the response
requestHeaders.forEach(function(x){
  var a = context.getVariable("request.header." + x );
  result[x] = a;
});

// set the response content:
context.setVariable('response.content', JSON.stringify(result, null, 2));

If you like, here is a plete proxy bundle that shows how to use this JS. Just deploy it into an Apigee org and invoke it like this:

curl -H x-custom-header:my-custom-value http://ORG-ENV.apigee/v1/ex-js-header

output:

{
  "Accept": "*/*",
  "Host": "ORG-ENV.apigee",
  "User-Agent": "curl/7.30.0",
  "x-custom-header": "my-custom-value"
}

You can access the request headers through your javascript using something like below

var request_headers = context.getVariable("request.headers.names");

The scope of this variable is only within the proxy request flow.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745084826a4610344.html

相关推荐

  • How to get all Apigee request headers in JavaScript? - Stack Overflow

    I have some JavaScript in which I need to work with all the request headers in Apigee. The online doc (

    19小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信