我有一些JavaScript,我需要使用Apigee中的所有请求标头。在线文档(http://apigee.com/docs/api-services/content/javascript-object-model)通过context.proxyRequest.headers作为名称/值对提及它们。我是否必须遍历它们,或者它们是否可以通过context.proxyRequest.headers作为数组集中使用?
感谢。
答案 0 :(得分:0)
您可以使用以下内容通过javascript访问请求标头
var request_headers = context.getVariable("request.headers.names");
此变量的范围仅在代理请求流中。
答案 1 :(得分:0)
此代码可供我查看所有请求标头:
// 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));
如果您愿意,here is a complete proxy bundle显示如何使用此JS。只需将它部署到Apigee组织中并像这样调用它:
curl -H x-custom-header:my-custom-value http://ORG-ENV.apigee.net/v1/ex-js-header
输出:
{
"Accept": "*/*",
"Host": "ORG-ENV.apigee.net",
"User-Agent": "curl/7.30.0",
"x-custom-header": "my-custom-value"
}