Testing Twilio SMS Requests for .NET Applications with the Rider REST Client

September 15, 2017
Written by
Maarten Balliauw
Contributor
Opinions expressed by Twilio contributors are their own

Rider tool for C#

We can use the Twilio Nuget package to write applications that work with the Twilio SMS API, but what if we want to experiment with the API in our IDE? If that IDE happens to be JetBrains Rider, there is an easy way to do this.

Rider is a cross-platform .NET IDE that builds on technology from ReSharper and IntelliJ IDEA. This provides the best of two worlds: excellent .NET code inspections, navigation and refactorings, plus the front-end features from a mature IDE with a vibrant ecosystem. Or in other words: if we want to test a Twilio SMS API call from within our IDE, we can use the built-in REST client for that.

After starting Rider and creating or opening a solution, open the REST client from Tools | Test RESTful Web Service. This will open a new tool window where we can craft HTTP requests.

This tool window allows us to select the HTTP method and set the host and path to be invoked. We can also add header data, query string parameters or a request body to be sent to the API.

Creating an HTTP request

Let’s see if we can send an SMS message from within our IDE. According to the API docs, we’ll need to POST to https://api.twilio.com/2010-04-01/Accounts/[AccountSid]/Messages and addFrom, To and Body parameters.

We could try invoking this request right now, but there is one key ingredient missing: authentication. Twilio requires us to use basic authentication using our AccountSid and AuthToken as credentials. We can find these in the Twilio Console and then use Rider’s generate credentials toolbar button to add the necessary header:

After clicking OK, Rider adds an Authorization header and we can now invoke our request. If all went well, we will see the result of our API call and our phone should notify us we have a new SMS message.

We got back an XML response but adding the Accept: application/json header will return JSON. The toolbar buttons allow us to reformat the response (which may be useful if the server stripped whitespace), open the response in a separate window, or export the response to a file. The various tabs let us inspect or add cookies, response headers etc.

Re-using requests

While Rider is running it keeps track of all requests that have been made previously. We can re-issue previous requests quite easily; just make sure not to spam yourself or a colleague with test SMS messages!

We can also export requests from the toolbar, including headers and request parameters, to a file that can be imported again later on. The generated files are XML files which we can hand-edit to our needs. Here’s an example send-sms.xml which contains the request above:

<RestClientRequest>
  <option name="biscuits">
    <list />
  </option>
  <option name="httpMethod" value="POST" />
  <option name="urlBase" value="https://api.twilio.com" />
  <option name="urlPath" value="/2010-04-01/Accounts/[AccountSid]/Messages" />
  <option name="headers">
    <list>
      <KeyValuePair>
        <option name="key" value="Accept" />
        <option name="value" value="*/*" />
      </KeyValuePair>
      <KeyValuePair>
        <option name="key" value="Cache-Control" />
        <option name="value" value="no-cache" />
      </KeyValuePair>
      <KeyValuePair>
        <option name="key" value="Authorization" />
        <option name="value" value="Basic [AccountSid]:[AuthKey]" />
      </KeyValuePair>
    </list>
  </option>
  <option name="parameters">
    <list>
      <KeyValuePair>
        <option name="key" value="To" />
        <option name="value" value="[Number]" />
      </KeyValuePair>
      <KeyValuePair>
        <option name="key" value="Body" />
        <option name="value" value="Testing Twilio from Rider" />
      </KeyValuePair>
      <KeyValuePair>
        <option name="key" value="From" />
        <option name="value" value="[Number]" />
      </KeyValuePair>
    </list>
  </option>
  <option name="parametersEnabled" value="true" />
  <option name="haveTextToSend" value="false" />
  <option name="haveFileToSend" value="false" />
  <option name="isFileUpload" value="false" />
  <option name="textToSend" value="" />
  <option name="filesToSend" value="" />
</RestClientRequest>

The export/import functionality allows us to create a series of HTTP requests that can be used during development and share with team members to quickly test common requests against the Twilio API.

Wrapping up

It’s quite useful to have a built-in REST client in an IDE like JetBrains Rider. We can test API calls against a service like Twilio while we are developing an application that uses these API’s.

Give it a try! And if you have any questions on Rider, hit me up on Twitter.