POST request in REST WCF -
i have developed rest wcf service method following:
[operationcontract] [webinvoke(method = "post", bodystyle = webmessagebodystyle.wrappedrequest, requestformat = webmessageformat.xml, responseformat = webmessageformat.xml, uritemplate = "/details")] detaildata getdetails(testdata requst); [datacontract] public class testdata { [datamember] public string detaildata { get; set; } }
now trying invoke service using following client code:
asciiencoding encoding = new asciiencoding(); string testxml = "<testdata>" + "<detaildata>" + "4000" + "</detaildata>" + "</testdata>"; string postdata = testxml.tostring(); byte[] data = encoding.getbytes(postdata); string url = "http://localhost/wcfrestservice.svc/bh/details"; string strresult = string.empty; // declare httpwebrequet wrt url defined above httpwebrequest webrequest = (httpwebrequest)webrequest.create(url); // set method post webrequest.method = "post"; // set content type webrequest.contenttype = "text/xml"; // set content length webrequest.contentlength = data.length; // stream data out of webrequest object stream newstream = webrequest.getrequeststream(); newstream.write(data, 0, data.length); newstream.close(); //gets response webresponse response = webrequest.getresponse(); //writes response stream responsestream = response.getresponsestream(); streamreader sr = new streamreader(responsestream); string s = sr.readtoend();
i getting following error :
"the remote server returned error: (400) bad request"
i call service method "get" verb being used. above client code invoking service using "post" verb not working. think, missing in client code. problem?
try changing
webmessagebodystyle.wrappedrequest
to
webmessagebodystyle.bare
Comments
Post a Comment