Page 1 of 1

Trouble http POST vs. GET

Posted: Sun Dec 28, 2014 4:32 pm
by Thomas Anderson7286790

I'm having an issue with handling an http 'POST' to my script. When I post to my script using an http 'GET', all works perfectly. Here is a sample of my code:

// Get request parameters

var requestParams = {},
paramKeys = request.keys();
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}

//Log parameters

console.log(JSON.stringify(request));

// Parse and set call values from request parameters

var CallSid = requestParams['CallSid'];

Here is the trace:

28.12.2014, 11:14:12 AM: {"params":{"AccountSid":"ACc4059745e60b37d8dd91a2a19199af5b","ToZip":"29579","FromState":"SC","Called":"+18433531264","FromCountry":"US","CallerCountry":"US","CalledZip":"29579","Direction":"inbound","FromCity":"CHARLESTON","CalledCountry":"US","CallerState":"SC","CallSid":"CA8ec4c1f8bff675a038ee2f0a900d3928","CalledState":"SC","From":"+18437371245","CallerZip":"29424","FromZip":"29424","ApplicationSid":"APbf3bb539960b0d3644b9a3f455520244","CallStatus":"ringing","ToCity":"MYRTLE BEACH","ToState":"SC","CallerName":"S CAROLINA CALL","To":"+18433531264","ToCountry":"US","CallerCity":"CHARLESTON","ApiVersion":"2010-04-01","Caller":"+18437371245","CalledCity":"MYRTLE BEACH"},"user":null,"method":"GET","requestBody":"","requestMimeType":null}

My code pulls 'CallSid' out of the request parameters perfectly when sent as a 'GET'.

Here is the trace when sent as a 'POST':

{"params":{},"user":null,"method":"POST","requestBody":"AccountSid=ACc4059745e60b37d8dd91a2a19199af5b&ToZip=29579&FromState=SC&Called=%2B18433531264&FromCountry=US&CallerCountry=US&CalledZip=29579&Direction=inbound&FromCity=CHARLESTON&CalledCountry=US&CallerState=SC&CallSid=CA305259034cadfee0f830b0537ee716a5&CalledState=SC&From=%2B18437371245&CallerZip=29424&FromZip=29424&ApplicationSid=APbf3bb539960b0d3644b9a3f455520244&CallStatus=ringing&ToCity=MYRTLE+BEACH&ToState=SC&CallerName=S+CAROLINA+CALL&To=%2B18433531264&ToCountry=US&CallerCity=CHARLESTON&ApiVersion=2010-04-01&Caller=%2B18437371245&CalledCity=MYRTLE+BEACH","requestMimeType":"application/x-www-form-urlencoded;charset=\"UTF-8\""""}

My code fails to pull 'CallSid'.

I'm a newbie with javascript and I'm sure it's an easy fix. Any suggestions?

Thanks!"


Trouble http POST vs. GET

Posted: Mon Dec 29, 2014 2:42 am
by Yurii Orishchuk

Hi Thomas,

As i can see your "CallSid" exists in two requests. See details: http://prntscr.com/5m2lhm/direct

Try following JS code to access this parameter when with post:

pre

var CallSid = request&#46;params['CallSid'];

/pre

Regards.


Trouble http POST vs. GET

Posted: Mon Dec 29, 2014 1:29 pm
by Thomas Anderson7286790

Not sure you understand based on your reply. There are not 2 requests. My twilio app is sending the request based on a phone call to my app. I can set twilio to send the request as either a GET or a POST. I gave you an example of each request.

My code works perfectly when twilio sends a GET request but fails when a POST request is sent.,

I tried your code and it does not work with POST. The trace shows:

Script AS_1_initial_greeting: Error: Empty objects can't be created. Please specify at least one field. ( @ 37 : 18 ) - throw e;

It appears to me that the body of the request in a POST is not being parsed by my code or yours.

I have shared the script with you.


Trouble http POST vs. GET

Posted: Mon Dec 29, 2014 1:50 pm
by Ray Phelan

I am having a similar problem with my app.
Can send by GET, but not by POST.
This only happened since last update.
Everything before the update was working fine.


Trouble http POST vs. GET

Posted: Mon Dec 29, 2014 11:08 pm
by Yurii Orishchuk

Hello,

Ok, i tried, and here is code to get post parameters:

precode

var postParameters = {};

var requestBody = request&#46;requestBody;
var bodyComponents = requestBody&#46;split("&");
for(var i = 0; i < bodyComponents&#46;length; i++){
var parameter = bodyComponents&#46;split("=");

Code: Select all

postParameters[parameter[0]] = parameter[1]; 

};

&#47;&#47;Using post parameters&#46; Check trace tab&#46;
console&#46;log(JSON&#46;stringify(postParameters));

/code/pre

Regards.


Trouble http POST vs. GET

Posted: Tue Dec 30, 2014 5:12 am
by Thomas Anderson7286790

Thanks. Got this to work finally with your help.