Access Request Body in a WCF RESTful Service

前端 未结 7 1003
不思量自难忘°
不思量自难忘° 2020-12-31 03:14

How do I access the HTTP POST request body in a WCF REST service?

Here is the service definition:

[ServiceContract]
public interface ITestService
{
          


        
相关标签:
7条回答
  • 2020-12-31 03:50

    The above answers helped me come up with this solution. I am receiving json with name/value pairs. {"p1":7514,"p2":3412, "p3":"joe smith" ... }

    [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        [WebInvoke(Method = "POST",
            BodyStyle = WebMessageBodyStyle.Bare, 
            RequestFormat = WebMessageFormat.Json
            )]
    
    public Stream getJsonRequest()
        {
    
            // Get the raw json POST content.  .Net has this in XML string..
            string JSONstring = OperationContext.Current.RequestContext.RequestMessage.ToString();
    
            // Parse the XML string into a XML document
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(JSONstring);
    
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                    node.Name // has key
                    node.InnerText;  // has value
    
    0 讨论(0)
提交回复
热议问题