How to read body of HttpRequest POSTed to a REST web service - C# WCF -


i have apex class (a class in salesforce) calls out rest web service.

public class webservicecallout  {     @future (callout=true)     public static void sendnotification(string astr)      {         httprequest req = new httprequest();         httpresponse res = new httpresponse();         http http = new http();          req.setendpoint('http://xx.xxx.xxx.xx:41000/testservice/web/test');         req.setmethod('post');         req.setheader('content-type', 'application/json');         req.setbody(astr); // want read in web service          try          {             res = http.send(req);         }          catch(system.calloutexception e)          {             system.debug('callout error: '+ e);             system.debug(res.tostring());         }     } } 

the rest web service (c#, wcf) looks so:

public interface itestservice {     [operationcontract]     [webinvoke(method = "post",      responseformat = webmessageformat.json,      bodystyle = webmessagebodystyle.bare,      uritemplate = "/test")]     string test(string astr); } 

the test() method primitive operations.

when run

webservicecallout.sendnotification("a test message") 

the post gets web service how can read set in body of httprequest req set in sendnotification() method - req.setbody(astr);?

that is, should parameter of string test(string astr); be?

do need specify else such configs/attributes in webinvoke or app.config (e.g. binding)?

if want read raw body of incoming request, should define type of parameter stream, not string. code below shows 1 way implement scenario, , post @ http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx has more information on "raw" mode.

public class stackoverflow_25377059 {     [servicecontract]     public interface itestservice     {         [operationcontract]         [webinvoke(method = "post",          responseformat = webmessageformat.json,          bodystyle = webmessagebodystyle.bare,          uritemplate = "/test")]         string test(stream body);     }      public class service : itestservice     {         public string test(stream body)         {             return new streamreader(body).readtoend();         }     }      class rawmapper : webcontenttypemapper     {         public override webcontentformat getmessageformatforcontenttype(string contenttype)         {             return webcontentformat.raw;         }     }      public static void test()     {         var baseaddress = "http://" + environment.machinename + ":8000/service";         var host = new servicehost(typeof(service), new uri(baseaddress));         var binding = new webhttpbinding { contenttypemapper = new rawmapper() };         host.addserviceendpoint(typeof(itestservice), binding, "").behaviors.add(new webhttpbehavior());         host.open();         console.writeline("host opened");          var req = (httpwebrequest)httpwebrequest.create(baseaddress + "/test");         req.method = "post";         req.contenttype = "application/json";         var reqstream = req.getrequeststream();         var body = "a test message";         var bodybytes = new utf8encoding(false).getbytes(body);         reqstream.write(bodybytes, 0, bodybytes.length);         reqstream.close();         var resp = (httpwebresponse)req.getresponse();         console.writeline("http/{0} {1} {2}", resp.protocolversion, (int)resp.statuscode, resp.statusdescription);         foreach (var header in resp.headers.allkeys)         {             console.writeline("{0}: {1}", header, resp.headers[header]);         }          console.writeline();         console.writeline(new streamreader(resp.getresponsestream()).readtoend());         console.writeline();     } } 

by way, incoming request not technically correct - (via content-type) you're sending json, request body (a test message) not valid json string (it should wrapped in quotes - "a test message" json string instead).


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -