Http Post from Fiddler giving nullpointreference error and cannot understand why

孤者浪人 提交于 2019-12-11 19:29:01

问题


When I am trying to test my HTTP post via fiddler my MVC4 Webapi gives me Null Pointer reference error right at the first If condition that is checking for the smsReq.Body. The smsReq is coming as NULL. What am I doing wrong?

Here is my Post Method and the fiddler request. I am using MVC4 Web Api

 using System.Web.Http;
 using System.Web;
 using Twilio.TwiML.Mvc;
 using Twilio.Mvc;

 public class ValuesController : ApiController
 {

    [HttpPost]
    public HttpResponseMessage Postsms([FromBody]SmsRequest smsReq)
    {

        if (String.IsNullOrEmpty(smsReq.Body.ToString()))
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));

        }
        else
        {

            string smsTextType = smsReq.Body.ToString();
            HttpResponseMessage response = this.Request.CreateResponse(HttpStatusCode.Created, "Hello World");
            return response;
        }

This is what my Post looks like from Fiddler:

  User-Agent: Fiddler
  Content-Type: application/json; charset=utf-8
  Accept: application/xml
  SmsSid: xxxx
  AccountSid: xxxxx
  From: xxxx
  To: xxxx
  Host: localhost:9999
  Content-Length: 5
  Body: abc

回答1:


Megan from the Twilio crew here.

As noted above, you must format your JSON like so:

[{ 
  message : 'Hello World',
  phonenumber : '+1415XXXXXXX'
}]

You can find this example in this blog post using the .NET Helper Library.



来源:https://stackoverflow.com/questions/18005546/http-post-from-fiddler-giving-nullpointreference-error-and-cannot-understand-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!