Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF HTTP POST Rest Service with Dictionary parameters

Tags:

rest

post

wcf

I am working on a WCF rest service that will need to take in an List of NAme value pairs of values. These values are not known yet, so that is why we need to use a generic list of name value pairs.

I have tried multiple methods, but I am not sure what the best way to do it is. I figured I would be able to access HttpContext and pull the values off the request body, but I am unable to do that.

What is the best way to have a WCF operation that takes in an HTTP Post with a List of Name Value pairs so that they can be read in the operation similar to how you would pull it off the Request["Key"]?

like image 239
TheJediCowboy Avatar asked Jul 03 '12 16:07

TheJediCowboy


1 Answers

An option would be to create a JSON object to post to the service with a format similar to:

{"kvPairs":[{"Key":"key1","Value":"value1"}, {"Key":"key2","Value":"value2"}]}

On the service side, set up a method similar to the following:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "")]
string DoSomething(Dictionary<string, string> kvPairs);
like image 76
Trey Combs Avatar answered Sep 18 '22 12:09

Trey Combs