当我试图通过测试提琴手我的HTTP后我MVC4的WebAPI让我空指针引用错误就在第一如果条件正在检查smsReq.Body。该SMSREQ即将为NULL。我究竟做错了什么?
下面是我的POST方法和提琴手请求。我使用MVC4网页API
使用System.Web.Http; 使用的System.Web; 使用Twilio.TwiML.Mvc; 使用Twilio.Mvc; 公共类ValuesController:ApiController { [HttpPost] 公众的Htt presponseMessage Postsms([FromBody] SmsRequest SMSREQ) { 如果(String.IsNullOrEmpty(smsReq.Body.ToString())) { 抛出新的Htt presponseException(Request.CreateResponse(的HTTPStatus code.BadRequest)); } 其他 { 字符串smsTextType = smsReq.Body.ToString(); HTT presponseMessage响应= this.Request.CreateResponse(的HTTPStatus code.Created,的Hello World); 返回响应; }这是我的帖子看起来就像小提琴手:
的User-Agent:提琴手 内容类型:应用程序/ JSON的;字符集= UTF-8 接受:应用/ XML SmsSid:XXXX AccountSid:XXXXX 来源:XXXX 要:XXXX 主机:本地主机:9999 内容长度:5 正文:ABC解决方案
梅根从Twilio剧组在这里。
如上所述,必须格式化你的JSON像这样:
[{ 消息:你好世界 电话号码:'+ 1415XXXXXXX}]您可以在这个博客帖子使用 .NET助手库
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解决方案
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.
更多推荐
从提琴手HTTP POST给nullpointreference错误,不明白为什么
发布评论