Trouble Getting Twilio SMS Response from ASP.NET Core MVC Endpoint


Trouble Getting Twilio SMS Response from ASP.NET Core MVC Endpoint



When I setup request bin, I get a response that seems totally normal (see screen shot below). When I use the command


ngrok http 5000



And I send the response to my local http endpoint, ngrok reports 200OK if my POST method in the controller has no parameters. Even if I add one parameter ([FromBody] string content), I get a 400 bad request out of ngrok's console.



I'm pasting below a couple different POST method's I've tried. I've tried inheriting my controller from controllerbase and controller and get the same behavior.


[HttpPost]
public string JsonStringBody([FromBody] TwilioSmsModel twilioSmsModel)
{
return "";
}


POST: api/SmsBody
[HttpPost]
public async Task<IActionResult> PostTwilioSmsModel([FromBody] TwilioSmsModel twilioSmsModel)
public async Task<IActionResult> Post()
{
var twilioSmsModel = new TwilioSmsModel();
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

_context.TwilioSmsModels.Add(twilioSmsModel);
await _context.SaveChangesAsync();

return CreatedAtAction("GetTwilioSmsModel", new { id = twilioSmsModel.SmsSid }, twilioSmsModel);
}



enter image description here



If there is a github example of sms notifications working with asp.net core 2.1, that would be a big help.




1 Answer
1



Twilio developer evangelist here. Most likely the error you're seeing has nothing to do with the way you're building your application, but with the fact that .NET expect some host headers to be passed in order to process your request. And why it works with requestbin.



You haven't specified any error message in your question, so this is guesswork, but try changing your ngrok commend to the following:


ngrok http 5000 -host-header="localhost:5000"



And you should stop seeing the 400 error you're getting and the requests should go through normally.


400



Hope this helps.





Thanks @Marcos, but still no luck. I did not include an error because I'm not sure how to find one. I set my statusCallback as follows: statusCallback: new Uri("bd621b2c.ngrok.io/api/sms"). The only error I see is in ngrok "400 Bad Request". I tried adding the -host-header and still errors. How to debug further?
– Peter Kellner
Jul 3 at 13:31





Per Corey Weathers, this solved the problem: public async Task<IActionResult> Post([FromForm] TwilioSmsModel twilioSmsModel) {
– Peter Kellner
Jul 3 at 16:20





Updating here since we solved this offline... It looks like your method is trying to retrieve the HTTP POST body from the request body. Changing it to [FromForm] should fix that.
– Corey Weathers
Jul 3 at 16:22


[FromForm]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages