Passing primitive type to WCF RESTful service
-
21-08-2019 - |
Question
I've been banging my head against the wall for the past couple of hours, here's what we're trying to do: a method expects a primitive/simple type as the request body. Originally we tried with a boolean, but that didn't work so we tried with string and object. Same thing.
Here's the server-side code
[OperationContract]
[WebInvoke(UriTemplate = "/foo/{foo_id}/bar", Method = "POST", ResponseFormat=WebMessageFormat.JSON)]
string G(string foo_id, string content);
And here's the request in Fiddler:
Header:
User-Agent: Fiddler
Host: localhost
Content-Type: 'application/json',
Content-Length: 19
Body:
"hello_world"
We tried to wrap "hello_world"
in a json object, like {"content":"hello_world"} but no luck.
Any thoughts?
Solution
Works fine for me, here's my code:
[OperationContract]
[WebInvoke(UriTemplate = "/foo/{foo_id}/bar", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public string G(string foo_id, string content)
{
return content + foo_id;
}
You didn't set the request format (a pain I know :))
Here's my Fiddler request:
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:54287
Content-Length: 7
"Hello"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow