从Web API方法获取原始POST数据 [英] Getting raw POST data from Web API method

查看:65
本文介绍了从Web API方法获取原始POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ApiController类中具有以下Web API方法:

I have the following Web API method in an ApiController class:

public HttpResponseMessage Post([FromBody]byte[] incomingData)
{
  ...
}

我希望 incomingData 是POST的原始内容.但是似乎Web API堆栈尝试使用JSON格式化程序解析传入的数据,这会导致客户端上的以下代码失败:

I want incomingData to be the raw content of the POST. But it seems that the Web API stack attempts to parse the incoming data with the JSON formatter, and this causes the following code on the client side to fail:

new WebClient().UploadData("http://localhost:15134/api/Foo", new byte[] { 1, 2, 3 });

有一个简单的解决方法吗?

Is there a simple workaround for this?

推荐答案

对于遇到此问题的其他任何人,解决方案是定义不带参数的POST方法,并通过 Request.Content :

For anyone else running into this problem, the solution is to define the POST method with no parameters, and access the raw data via Request.Content:

public HttpResponseMessage Post()
{
  Request.Content.ReadAsByteArrayAsync()...
  ...

这篇关于从Web API方法获取原始POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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