将字符串参数发布到WCF服务会导致反序列化错误

Jea*_*ois 2 .net rest wcf json

我正在尝试将json字符串发布到.net wcf服务.

这是操作的定义:

[WebInvoke(Method = "POST", UriTemplate = "test/")]
[OperationContract]
[Description("Test")]
void Test(string input);
Run Code Online (Sandbox Code Playgroud)

我用fiddler发送我的测试.这里有小提琴信息:

POST http://localhost/test.svc/test/ HTTP/1.1
Content-Type: application/json
Host: localhost
Content-Length: 4

test
Run Code Online (Sandbox Code Playgroud)

我总是收到这个http 400错误:

反序列化System.String类型的对象时出错.令牌"真实"是预期的,但发现'测试'.

我究竟做错了什么?我敢肯定这可能是非常明显的事情,但我从今天早上就开始......

这个问题已被分解为最简单的表达方式.在现实世界中,我们想要发布一个实际上是JSON的字符串.但我们不希望.net处理反序列化,我们希望自己在服务中自己完成.

car*_*ira 7

请求内容

test
Run Code Online (Sandbox Code Playgroud)

是无效的JSON.您需要在引号内发送字符串:

POST http://localhost/test.svc/test/ HTTP/1.1
Content-Type: application/json
Host: localhost
Content-Length: 6

"test"
Run Code Online (Sandbox Code Playgroud)