如何在代理请求流中为json数据注入新的请求标头?

时间:2014-06-20 07:31:49

标签: apigee

我正在尝试在代理请求流中注入一个新的请求标头,使用JS策略发送到后端服务器。当我查看调试跟踪时,我看到请求头中的json数据失真。

我正在尝试注入一些像

这样的字符串
{"scope":"","time_till":2264,"id_1":"hUXLXVqpA1J4vA9sayk2UttWNdM","custom_data":{"c_id":"test_data"}}

但是当我看到跟踪窗口时,我看到了这个

{"scope":"","time_till":2264,id_1":"hUXLXVqpA1J4vA9sayk2UttWNdM,"custom_data":{"c_id":"test_data"}} 

我做错了什么?

var obj = {"scope":"","time_till":2264,"id_1":"hUXLXVqpA1J4vA9sayk2UttWNdM","custom_data":{"c_id":"test_data"}};
var header_str = JSON.stringify(obj);
context.setVariable('json-header',header_str);
request.headers['x-json-hedar']= header_str;

3 个答案:

答案 0 :(得分:0)

我测试了你的代码,它似乎工作。这是一个示例响应,我将标题字符串设置为响应:

HTTP/1.1 200 OK
User-Agent: curl/7.30.0
Accept: */*
x-json-header: {"scope":"","time_till":2264,"id_1":"hUXLXVqpA1J4vA9sayk2UttWNdM","custom_data":{"c_id":"test_data"}}
Content-Length: 0

看起来这只是Apigee调试会话/跟踪工具的一个问题,因为标头值设置正确。以下是调试会话的JSON下载,显示了此标头值:

{
    "name": "x-json-header",
    "value": "{\"scope\":\"\",\"time_till\":2264,id_1\":\"hUXLXVqpA1J4vA9sayk2UttWNdM,\"custom_data\":{\"c_id\":\"test_data\"}}"
}

您可以看到传递给用于显示调试信息的UI的值具有格式错误的json:

id_1\":\"hUXLXVqpA1J4vA9sayk2UttWNdM,

答案 1 :(得分:0)

这似乎不是Apigee调试/跟踪UI的问题。我看到格式错误的JSON流向我的后端服务。

这是我要发送的标题 -

{"timeStamp":"2349218349381274","latitude":"34.589","longitude":"-37.343","clientIp":"127.0.0.0","deviceId":"MOBILE_TEST_DEVICE_AGAIN","macAddress":"23:45:345:345","deviceType":"phone","deviceOS":"iOS","deviceModel":"iPhone 5S","connection":"5G","carrier":"Vodafone","refererURL":"http://www.google.com","xforwardedFor":"129.0.0.0","sessionId":"kfkls498327ksdjf","application":"mobile-app","appVersion":"7.6.5","serviceVersion":"1.0","userAgent":"Gecko"}

但Apigee读取标题如下。请注意某些字段中缺少的起始引号。

{"timeStamp":"2349218349381274",latitude":"34.589,longitude":"-37.343,clientIp":"127.0.0.0,deviceId":"MOBILE_TEST_DEVICE_AGAIN,macAddress":"23:45:345:345,deviceType":"phone,deviceOS":"iOS,deviceModel":"iPhone 5S,connection":"5G,carrier":"Vodafone,refererURL":"http://www.google.com,xforwardedFor":"129.0.0.0,sessionId":"kfkls498327ksdjf,application":"mobile-app,appVersion":"7.6.5,serviceVersion":"1.0,"userAgent":"Gecko"}

标头用于对后端服务的服务标注,后端服务对其进行解析。这是正确的,我得到以下错误 -

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('l' (code 108)):      was expecting double-quote to start field name
at [Source: java.io.StringReader@22549cdc; line: 1, column: 35]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1378)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:599)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:520)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleUnusualFieldName(ReaderBasedJsonParser.java:1275)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._parseFieldName(ReaderBasedJsonParser.java:1170)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:611)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:301)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2796)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1942)

答案 2 :(得分:0)

将JSON添加到上下文变量时遇到奇怪的行为,例如:

 var header_str = JSON.stringify(obj);
 context.setVariable('json-header',header_str);

我很欣赏这是一个例子,所以你可能没有包含问题的全部范围,但这通常有效(现在它不会首先添加到变量中):

request.headers['x-json-header'] = JSON.stringify(obj);

如果你可以从JavaScript发送请求

,这样的代码也有效
var headers = {"Accept": "application/json", "Accept-Language": "en"};
var sessionRequest = new Request(url, 'POST', headers, body);
var exchange = httpClient.send(sessionRequest);
exchange.waitForComplete()
if (exchange.isSuccess()){
    var responseObj = exchange.getResponse().content.asJSON;
    if (responseObj.error){
        request.content += JSON.stringify(responseObj);
    }
}

此外,我成功使用AssignMessage策略构建请求,然后使用Callout策略读取存储的请求,然后发出该请求并将结果存储在响应对象中,然后可以通过Extract读取变量政策。