从提琴手HTTP POST给nullpointreference错误,不明白为什么

编程入门 行业动态 更新时间:2024-10-25 17:16:18
本文介绍了从提琴手HTTP POST给nullpointreference错误,不明白为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我试图通过测试提琴手我的HTTP后我MVC4的WebAPI让我空指针引用错误就在第一如果条件正在检查smsReq.Body。该SMSREQ即将为NULL。我究竟做错了什么?

下面是我的POST方法和提琴手请求。我使用MVC4网页API

使用System.Web.Http; 使用的System.Web; 使用Twilio.TwiML.Mvc; 使用Twilio.Mvc; 公共类Values​​Controller: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错误,不明白为什么

本文发布于:2023-10-10 22:43:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479966.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不明白   错误   提琴手   POST   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!