使用url编码的JSON有效负载转换POST

时间:2014-06-25 22:45:13

标签: javascript json api apigee

好吧,我没有把这一个关掉,所以我们会看到我是否错过了这个显而易见的东西。

根据请求

curl "https://api.me.com/v1/visitors" --data  "visitor=%7B%0A++%22funnels%22%3A+%7B%7D%2C%0A++%22_partition%22%3A+96%2C%0A++%22metric_sets%22%3A+%7B%0A++++%2234%22%3A+%7B%0A++++++%22safari%22%3A+1%0A++++%7D%0A++%7D%2C%0A++%22flags%22%3A+%7B%0A++++%22Book+Pack+Purchaser%22%3A+false%2C%0A++++%22Boat+PP+Viewer%22%3A+true%2C%0A++++%22Boat+Purchaser%22%3A+false%2C%0A++++%22Mobile+Shopper%22%3A+true%2C%0A++++%22Visitor+Buy%22%3A+false%2C%0A++++%22Testing%22%3A+true%2C%0A++++%22Book+Pack+PP+Viewer%22%3A+false%2C%0A++++%22Women%27s+Dept+Visitors%22%3A+true%2C%0A++++%22Boat+Abandoner%22%3A+false%0A++%7D%2C%0A++%22replaces%22%3A+%5B%5D%2C%0A++%22shard_token%22%3A+21000096%0A%7D"

你会注意到--data传递的是一个已编码的JSON对象。

 {"not":"my design"}

你可以去这里编码:http://www.url-encode-decode.com/基本上变成了:

{
 "funnels": {},
"_partition": 96,
"metric_sets": {
  "34": {
    "safari": 1
  }
},
"flags": {
  "Book Purchaser": false,
  "Boat PP Viewer": true,
  "Boat Purchaser": false,
  "Mobile Shopper": true,
  "Visitor Buy": false,
   "Testing": true,
  "Book Pack PP Viewer": false,
  "Women's Dept Visitors": true,
  "Boat Abandoner": false
 },
"replaces": [],
"shard_token": 21000096
}

好吧,我无法弄清楚如何将值放入脚本中的JSON对象中,以便我可以开始混搭数据。我有一个......

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="Script-ProcessRequestVars">
    <DisplayName>Script-ProcessRequestVars</DisplayName>
        <FaultRules/>
       <Properties/>
    <ResourceURL>jsc://Script-ProcessRequestVars.js</ResourceURL>
</Javascript>

用......

var processRequestPayload = function(){
       context.setVariable("my.requestVerb",context.proxyRequest.method);
       context.setVariable("my.visitor",context.proxyRequest.body.asForm['visitor'][0]);

       var visitObj = JSON.parse(context.proxyRequest.body.asForm['visitor'][0]);
       var shard = visitObj.shard_token;

};

function init() {
  processRequestPayload();
}

init();

但我得到的只是......

 {
   "fault": {
   "faultstring": "Execution of Script-ProcessRequestVars failed on line 4 with error: 1",
   "detail": {
     "errorcode": "steps.javascript.ScriptExecutionFailedLineNumber"
      }
   }
 }

我错过了什么?

2 个答案:

答案 0 :(得分:2)

克里斯,试着替换:

context.setVariable("my.visitor", context.proxyRequest.body.asForm['visitor'][0]);

使用

context.setVariable("my.visitor", request.body.asForm['visitor'][0]);

context.proxyRequest.body似乎存在问题。如果通过上述解决方法解决了此问题,请打开票证,同时使用此解决方法。

答案 1 :(得分:0)

我不确定你为什么要将你的JSON作为表单参数传递...我个人会将主体发送为Content-Type:application / json并使用ExtractVariables策略中的JSON Path来设置Apigee变量你想要操纵的JSON:

POST
Content-type: application/json

{
 "funnels": {},
"_partition": 96,
"metric_sets": {
  "34": {
    "safari": 1
  }
},
"flags": {
  "Book Purchaser": false,
  "Boat PP Viewer": true,
  "Boat Purchaser": false,
  "Mobile Shopper": true,
  "Visitor Buy": false,
   "Testing": true,
  "Book Pack PP Viewer": false,
  "Women's Dept Visitors": true,
  "Boat Abandoner": false
 },
"replaces": [],
"shard_token": 21000096
}

ExtractVariables示例:

<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1">
    <DisplayName>Extract Variables 1</DisplayName>
    <FaultRules/>
    <JSONPayload>
        <Variable name="name">
            <JSONPath>$.flags.Book%20Purchaser</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>apigee</VariablePrefix>
</ExtractVariables>

BTW - 您可能还想避免在JSON标签中放置空格 - 这不是真正的标准,并且可能导致解析问题......

ExtractVariables http://apigee.com/docs/api-services/content/extract-message-content-using-extractvariables