简体   繁体   中英

How to set HTTP cookies with Twilio and Node?

I'm trying to use cookies to track SMS conversations with my node app, but they never seem to get set correctly. At least, when I retrieve them in the next text from the user, they're still null.

I have this function where it sets a cookie and sends a response to the user:

 function requestLocation (res, resend) { var locationXml = ""; ... var responseText = ""; ... var response = new twilio.TwimlResponse(); response.sms(responseText); res.cookie('rideStage', rideStages.SENT_REQUEST); res.send(response.toString(), { 'Content-Type':'text/xml' }, 200); } 

But when I receive the next message from that user, "request.cookies.rideStage" is always null. I've also tried replacing

 res.cookie('rideStage', rideStages.SENT_REQUEST); res.send(response.toString(), { 'Content-Type':'text/xml' }, 200); 

with

 res.send(response.toString(), { 'Set-Cookie':'rideStage='+rideStages.SENT_REQUEST, 'Content-Type':'text/xml' }, 200); 

but, request.cookies.rideStage is still null.

To anyone seeing this, what I posted above is actually correct.

By doing: res.cookie('cookieName', cookieVal);

You can then use: req.cookies.cookieName

to get cookieVal. The reason it wasn't working for me is because I was just testing my app locally and simulating a text with curl in the terminal, which totally excluded Twilio and cookie handling from the entire process (whoops).

For the curious, we've also got a blog post about tracking SMS conversations with cookies.

And if you're looking to give it a try in Node get started with the helper library but don't forget to use something like ngrok to allow Twilio to see your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM