2

Okay, I'm not getting this one right off so we'll see if I'm missing the obvious.

Given the request

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"

You'll notice that the --data passed is a JSON object that has been encoded.

 {"not":"my design"}

You could go here to encode it: http://www.url-encode-decode.com/ basically it turns into:

{
 "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
}

Well I can't figure out how to get the value into a JSON object in a Script so I can start mashing up the data. I got a...

<?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>

With a...

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();

But all I get is a...

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

What am I missing??

klevak
  • 63
  • 4

2 Answers2

2

Kris, try replacing:

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

With

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

There seems to be an issue with context.proxyRequest.body. If this issue gets fixed with above workaround, please open a ticket and in the meantime use this workaround.

Diego
  • 1,678
  • 1
  • 16
  • 20
0

I'm not sure why you're passing your JSON as a form parameter... Personally I would send the body as Content-Type: application/json and use the JSON Path in an ExtractVariables policy to set Apigee variables for the JSON you want to manipulate:

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 example:

<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 -- you might also want to avoid putting spaces in your JSON labels -- not really standard and can cause parsing problems...

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

Michael Bissell
  • 1,210
  • 1
  • 8
  • 14
  • 1
    Passing JSON as a form parameter is a BAD design in my opinion as well: {"not":"my design"} Some Vendor thought it was a good idea. – klevak Jun 27 '14 at 16:00
  • Also, spaces in json labels are a smelly practice as well. You bet I'll be transforming all this sorta cruft out in my new facade. – klevak Jun 27 '14 at 16:36