问题
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