JSON parsing error when POSTing directly to function webhook URL

372 views
Skip to first unread message

Roy Kiesler

unread,
Sep 12, 2017, 6:02:38 PM9/12/17
to Twilio Functions
I am sending this JSON message as an HTTP POST directly to my function webhook URL:

{
  "apikey": "***",
  "status": "active",
  "member": {
    "phone": "(925) 876-6323"
  },
  "plan": {
    "name": "HR Plan",
    "limits": [
      {
        "ceiling": 2
      },
      {
        "ceiling": 5
      }
    ]
  }
}

In my function, I obtain this message from the event parameter:

    var keys = Object.keys(event);
    var body = keys[0].replace(/\n/g, " ").replace(/_/g, " ");  // the JSON is serialized with hard-coded
    var req = JSON.parse(body);

This results in the following error:

{ message: 'Unexpected end of JSON input', name: 'SyntaxError', stack: 'SyntaxError: Unexpected end of JSON input\n at Object.parse (native)\n at Object.exports.handler (/var/task/handlers/ZF751859f3010f5f06bd05eab117ba72dd.js:13:20)\n at Object.exports.handler (/var/task/node_modules/enigma-lambda/index.js:268:10)\n at exports.handler (/var/task/enigma.js:17:9)' }

However, if I remove the nested limits array from the message:

{
  "apikey": "***",
  "status": "active",
  "member": {
    "phone": "(925) 876-6323"
  },
  "plan": {
    "name": "HR Plan"
  }
}

the error goes away and the function logic executes just fine. This is a perfectly valid JSON object, so I cannot rationalize this error.

Chris Corcoran

unread,
Sep 13, 2017, 5:56:20 PM9/13/17
to Twilio Functions
Hey Roy,

I'm sorry you're running into issues with Twilio Functions. I've dug into the issue and I think I found the problem.

The first thing I tried was creating a Function with the lines of code you provided and the problematic JSON payload. In that scenario I am able to replicate your error. However, then I try the same code against your second JSON payload I do not get a successful request. In fact, in the second scenario I see the same error as with the original problematic JSON payload. This lead me to believe the error isn't with Twilio Functions.

I dug into your code and I think I found the issue. As is your code is attempt to JSON parse the first key returns by Object.keys(). I've annotated the snippet you provided to illustrate what is happening.
var keys = Object.keys(event); // [ "apikey", "status", "member", "plan"]

var body = keys[0].replace(/\n/g, " ").replace(/_/g, " ");  // "apikey".replace(/\n/g, " ").replace(/_/g, " ") => "apikey"

var req = JSON.parse(body); // JSON.parse("apikey")


It seems like you are trying to parse the HTTP request body into JSON object. Luckily you don't need to do that with Twilio Functions. When you provide the appropriate Content-Type then Functions will populate the event object with your JSON payload. So, to access the API Key field from your request all you need to do is "event.apikey". 

Hopefully, this helped unblock you. Please let me know if you have any other questions or feedback.

Thanks,
Chris Corcoran
Product Manager, Twilio Functions

Roy Kiesler

unread,
Sep 13, 2017, 6:57:31 PM9/13/17
to Twilio Functions
Thanks, Chris -- simple and helpful clarification. 

A quick follow-up, if you will. Is there a way to access the HTTP request metadata from the event? Specifically, I'm interested in any parameters passed to the webhook URL, e.g., https://fancy-can-****.twil.io/function?field1=value1

Thanks again,
--
Roy
Reply all
Reply to author
Forward
0 new messages