Twilio's RAW HTTP POST request for status updates and message responses with all attribute names? - twilio

The documentation Twilio has on their website doesn't contain raw POST bodies, and it has conflicting sources on how the attributes are named/which attributes are included in the body.
Field names and their casing don't match:
https://www.twilio.com/docs/api/twiml/sms/twilio_request#request-parameters
https://www.twilio.com/docs/api/rest/message
Should I expect Message Resource type for all of their POST requests to our URLs(Status Updates and Message Responses) ?
Should I expect only the field names advertised on their Twilio's request page?
Do they provide all parameters or any additional parameters in their request not advertised on the documentation(Twilio's request)
I'm assuming they would also provide "MessageStatus" field in their POST requests, but I don't see it on all documentation pages.
The headers in their requests?

Twilio developer evangelist here.
Of the two pieces of documentation you are referring to there, only one is about the request Twilio makes to you when you receive an SMS message. The other is the Messages resource of the REST API.
So, in answer to your questions:
1) The attributes you receive are the ones described in the request documentation. If you want to confirm this, then I recommend setting your webhook URL to a Request Bin which is an easy way to inspect a webhook request like this. Alternatively, if you are using ngrok to test webhooks locally, then check out the ngrok dashboard which also allows you to inspect (and replay) requests.
2) Yes!
3) There are no extra parameters that I know of.
4) MessageStatus is not included. You can look it up using the Messages resource, but in my experience at the point of receiving the webhook the status is "receiving". Once you are done processing the webhook the status changes to "received".
5) The headers are mostly standard. There are extra ones for security, such as the X-Twilio-Signature header. Again, I recommend inspecting a request with Request Bin or ngrok to fully understand the entire request.
Let me know if this helps at all.
Edit
The Content-Type header for POST requests from Twilio is application/x-www-form-urlencoded.
In a message status callback, the parameters are all the usual parameters plus the MessageSid, a MessageStatus, and if relevant an ErrorCode.

Related

Twilio URL example for sending MMS

I'm a very new programmer and am messing around with creating URL's to send SMS or MMS messages via Twilio (my application is for home automation where if my camera detects motions, I want to send a still image to an MMS number via a URL).
Can someone post the format for sample URL for an MMS message that I can paste in a browser?
For example, if I have a To parameter of To=+7145551212 and a From parameter of From=+7145551111 and MediaURL=http://test.com/image.jpg, what would the format of the URL need to be?
I have my Account SID and AuthToken as well. Just need an example of what a completed URL would look like so I can work backwards from there.
Thanks!
Paul
If I'm understanding you correctly, I think it would be this :
https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages
And then your parameters need to be in the POST body of the request, like
To=+7145551212&From=+7145551&MediaURL=http://test.com/image.jpg
But Brodan's comment is correct- you can't do this through just pasting a URL into your browser, because it's a POST request. You could use cURL, something like this :
curl --data "To=+7145551212&From=+7145551&MediaURL=http://test.com/image.jpg" https://api.twilio.com/2010-04-01/Accounts/123456/Messages
But your best option would really be to use one of the Twilio helper libraries. Here's a link to their description of the endpoint, they have examples of using those libraries in a variety of languages.
Note : I have never used Twilio myself.

Sending parameter with POST requests ,when content-type is multipart/form-data

I am new to JMeter. I am trying to create a test plan ,one of the requests is a POST request containing some parameter, the content type in the request header is Multipart/Form-data.
I am copying the headers/parameters from fiddler because the HTTP proxy recorder is not working.
Please see the image for the current settings I have.I am not able to get the required response using it.
As per HTTP Request Sampler Documentation
Use multipart/form-data for HTTP POST
Use a multipart/form-data or application/x-www-form-urlencoded post request
So all you need to do is:
Tick "Use multipart/form-data for POST" box
Remove all `Content-Disposition" lines
In regards to "proxy recorder not working", I have never experienced any problems with it so it might be misconfiguration or something like this. Some people find JMeter Chrome Extension easier to use.
Instead of copying the content-disposition etc, just send the parameters with name and you should be good. You are expected to send form data and it's value.
ideally it should look like, name should be just 'form' and it's value as 'buy-now'.
I would suggest you compare the requests that you are sending using developer tools and the request you are sending using JMeter, it will help you debug this quicker.
I hope it helps.

Redirect a http post request with modified http header to another server

I'm using Ruby on Rails. Here is the requirement: the client (a native mobile app developed by me) will send a http post request to my Ruby code, my code will add some extra http headers (based on some business logic), then I need to "forward" or "redirect" this post request to another backend server (which has a REST service) and return its response back to the client.
I have been able to write a rack middleware to intercept the post request and add the extra headers. Originally I thought I could just use http redirect (status code: 307 for post request). But the problem is that the extra headers could NOT be submitted, which is the whole point of my code. So this isn't http redirect or forwarding per se, it's more like transforming a request.
I'm able to make a separate post request from my code using net http. This works. But I have to COPY data from the incoming request to my outgoing request (eg form data, http headers). This copying seems a bit tedious.
I would prefer some kind of simple "repackaging" (which is akin to http redirect or forwarding), that is I copy the whole incoming request to the outgoing request, slap on the extra headers and send it to the destination URL and be done with. I am not sure how to do this, and if doing it this way is even a good idea. For example, HTTP_USER_AGENT shows the OS, browser type of the client, when I'm making a new request, I probably don't need to send this on.
Alternatively, I can copy only the application specific data, because they're all the backend server (the destination of this "redirect") cares about. But I'm averse to hardcoding attributes in my code, causing close-coupling with the client (our native mobile app). Ideally I only copy application-specific data without hardcoding their attribute names. Is this possible? If so, how?
Any advice would be appreciated.
Thank you.
HTTP does not allow redirects for anything other than GET request.
(This is not technically correct but using HTTP 307 is kind of sketchy - see https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect)
If you need too send a POST request to another server for processing then using a proxy as you already seem to be doing is the correct solution.
Recreating the request in the proxy may seem tedious but it actually serves as a guarantee that you are calling the other servers "API" correctly.
While you can simply loop through the request headers:
uri = URI('http://www.example.com/todo.cgi')
req = Net::HTTP::Post.new(uri)
request.headers.each do |key, value|
req[key] = value
end
And pass the request form data:
req.set_form_data = request.request_parameters
You should ask yourself if it really is prudent to proxy everything.
See http://api.rubyonrails.org/classes/ActionDispatch/Request.html

How can I retrieve and save Twilio responses in Rails?

So a user calls a Twilio phone number. Twilio looks for a voice_url attached to that number (from their dashboard) and sees some XML with instructions on how to handle the call. The XML file also has an "action" parameter that points to a url.
Ideally, this URL should be able to retrieve parameters sent by Twilio and save them to a DB. This is where I'm stuck; how can I see which parameters are sent and how can I save them? I'm assuming the URL in the "action" parameter points to a controller?
I'm using the twilio-rb gem.
Some relevant links:
http://www.twilio.com/docs/api/twiml/dial#attributes-action
http://www.twilio.com/docs/api/twiml/twilio_request
When Twilio makes a POST to the URL mentioned by the Action parameter, it will send all of the attributes mentioned here: http://www.twilio.com/docs/api/twiml/twilio_request.
You should create your own route and controller for responding to the incoming Twilio request. When Twilio makes the request, you should be able to get the variables out of the POST request like any POST request. See for example How to access POST variables in Rails?.

Pubsubhubbub on Rails. How to extract the raw POST body contents from the POST request?

I am having trouble setting up a pubsub enabled subscriber app using rails. I have currently subscribed to the open hub pubsubhubbub.appspot.com and am receiving pings to my application's endpoint. (as of now i have created a counter which increments everytime the end point is pinged). But i am not able to understand as to how to extract the raw POST body contents from the POST. I am new to pubsub and am eager to experiment with it. I came across this blog post but it is not language specific.
Source: Joseph Smarr: Implementing PubSubHubbub subscriber support: A step-by-step guide. http://josephsmarr.com/2010/03/01/implementing-pubsubhubbub-subscriber-support-a-step-by-step-guide/
Now you’re ready for the
pay-out–magically receiving pings from
the ether every time the blog you’ve
subscribed to has new content! You’ll
receive inbound requests to your
specified callback URL without any
additional query parameters added
(i.e. you’ll know it’s a ping and not
a verification because there won’t be
any hub.mode parameter included).
Instead, the new entries of the
subscribed feed will be included
directly in the POST body of the
request, with a request Content-Type
of application/atom+xml for ATOM feeds
and application/rss+xml for RSS
feeds. Depending on your programming
language of choice, you’ll need to
figure out how to extract the raw POST
body contents. For instance, in PHP
you would fopen the special filename
php://input to read it.
Any help would be greatly appreciated.
You didn't say but I'm assuming you are running Rails 3.x?
To get the raw POST body you simply use request.raw_post in your controller. This will give you a long string that looks like a request parameters string: some_var=something&something_else=something_else... which you can then parse to get at what you want.
However, look at you development logs for an incoming request and see if the params hash isn't a better option for you. The service should post the data under some variable name, such as some_var above, and the params hash will hold an params[:some_var] containing only that data. No need for you to dig it out on your own in other words.

Resources