Reputation: 529
When twilio is trying to access my REST API URL via POST for an incoming SMS I am seeing the error.
"An attempt to retrieve content from returned the HTTP status code 502. Please check the URL and try again.
What is this error and how to fix it? Is the error coming from my server or Twilio side? Am I missing something on my side? Looking at Twilio web site as given here:
http://www.twilio.com/docs/errors/11200
it says something about we need to set the Content-Header. So how do I do that? New to web Api and twiml.
Edited:
This is what I have currently under my webapi.config
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
My route config has
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I added the following in Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configuration.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "text/xml");
GlobalConfiguration.Configuration.Formatters.XmlFormatter.AddUriPathExtensionMapping("json", "application/json");
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
If I add {action}.ext in my webapi.config I am getting 404 error. So now what did I miss :-(
Upvotes: 0
Views: 1975
Reputation: 10366
Twilo evangelist here.
So Web API determines how to serialize the data returned from an endpoint (JSON, XML, etc) by looking at the Accept header in the incoming request. It will also set the Content-Type header based on the format of data it returns. The problem is that Twilio's web hook requests don't include an Accept header, and if there is no Accept header Web API defaults to returning JSON.
Twilio expects TwiML (our XML-based language) in response to to its request, so if your Web API endpoint is returning JSON Twilio and setting the Content-Type header in the response to application/json, Twilio says thats bad.
There are a couple different ways you can tell Web API to format the response as XML. The first is to remove the JSON formatter as an option available to Web API. This SO post shows you how to remove the json media type formatter from the collection of available formatters:
Disable JSON Support in ASP.NET MVC Web API
Another option is to tell Web API to use a file extension as the way to determine which formatter to use, instead of the Accept header by using the AddUriPathExtensionMapping method:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "text/xml");
That line tells Web API to treat any request to its endpoints that have a .xml extension to as a request for a text/xml media type response.
If you do this, you have to update your Web API route as well to allow for the extension:
api/{controller}/{action}.{ext}/{id}
Hope that helps.
Upvotes: 1