错误在POST发送json的对Web API服务 [英] Error sending json in POST to web API service

查看:2981
本文介绍了错误在POST发送json的对Web API服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建使用Web API的Web服务。我实现了一个简单的类

I'm creating a web service using Web API. I implemented a simple class

public class ActivityResult
{
    public String code;
    public int indexValue;
    public int primaryCodeReference;
}

然后,我有我的控制器内实施

And then I have implemented inside my controller

[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
    return new HttpResponseMessage(HttpStatusCode.OK);
}

但是,当我调用API传递POST文件JSON:

But when I call the API passing in POST the file json:

{"code":"XXX-542","indexValue":"3","primaryCodeReference":"7"}

我得到了以下错误消息:

I obtain the following error message:

{
    "Message": "The request entity's media type 'text/plain' is not supported for this resource.",
    "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'ActivityResult' from content with media type 'text/plain'.",
    "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
    "StackTrace": "   in System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   in System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   in System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}

我是什么做错了吗?

What am I doing wrong?

推荐答案

在您需要设置内容类型的HTTP请求:内容类型:应用程序/ JSON

In the HTTP request you need to set Content-Type to: Content-Type: application/json

因此​​,如果您使用招客户端添加内容类型:应用程序/ JSON 来请求头

So if you're using fiddler client add Content-Type: application/json to the request header

这篇关于错误在POST发送json的对Web API服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆