简体   繁体   中英

400 Bad Request Received from WCF Rest Service - XML formatted request

I've been having trouble with a web service hosted locally.

Here's the parameter that the web service accepts:

    [DataContract(Namespace = "http://com.rest")]
    [XmlRootAttribute(ElementName = "PlaceCutEventData", Namespace = "http://com.rest", IsNullable = false)]
    public class PlaceCutEventData
    {
        [DataMember(IsRequired = true)]
        public string CutName { get; set; }
        [DataMember(IsRequired = true)]
        public string CutID { get; set; }
        [DataMember(IsRequired = true)]
        public string StationName { get; set; }
        [DataMember(IsRequired = true)]
        public string LineID { get; set; }
        [DataMember]
        public double CutLength { get; set; }
        [DataMember]
        public string Comment { get; set; }
        [DataMember]
        public string GeoName { get; set; }
        [DataMember]
        public bool IsClosed { get; set; }
        [DataMember(IsRequired = true)]
        public string Phases { get; set; }

        public PlaceCutEventData()
        {
            SetDefaults();
        }

        [OnDeserializing]
        private void OnDeserializing(StreamingContext context)
        {
            SetDefaults();
        }

        private void SetDefaults()
        {
            CutLength = 0.50;
            IsClosed = true;
            GeoName = "DETAIL";
        }
    }

Here's what the interface looks like (It's already implemented):

        [WebInvoke(UriTemplate = "/PlaceCut", Method = "POST")]
        [OperationContract]
        [Description("Request to place a cut")]
        Task<ServerResponse> PlaceCut(PlaceCutEventData data);

I'm able to send a request to the web service with a request in JSON format, and it actually process it. However, when i switch to XML format, it will always return 400 Bad Request:

Here's the XML:

<PlaceCutEventData xmlns="http://com.rest">
    <CutName>testing</CutName>
    <CutID>13213</CutID>
    <StationName>Test</StationName>
    <LineID>TestID</LineID>
    <CutLength>0.60</CutLength>
    <Comment></Comment>
    <GeoName>TestGeoName</GeoName>
    <IsClosed>true</IsClosed>
    <Phases>N</Phases>
</PlaceCutEventData>

Here's the json version, but my web service actually accepts this request:

{
    "CutName": "testing",
    "CutID": "13213",
    "StationName": "Test",
    "LineID": "TestID",
    "GeoName": "TestName",
    "Phases": "N",
    "IsClosed": true,
    "CutLength": 0.60,
    "Comment": ""
}

Can someone please explain why the XML format is not working?

Thank you very much.

There seems no problem in the XML format. Here is my request. 在此处输入图片说明
I changed the Datatype from JSON to XML, so as to add the below HTTP header.

Content-Type: application/xml

Here is my definition of the function and Data contract.

        [OperationContract]
        [WebInvoke]
        string PlaceCut(PlaceCutEventData data);
    }

    [DataContract(Namespace = "CustomDataContract")]
    public class PlaceCutEventData
    {
        [DataMember(IsRequired = true)]
        public string CutName { get; set; }
        [DataMember(IsRequired = true)]
        public string CutID { get; set; }
        [DataMember(IsRequired = true)]
        public string StationName { get; set; }
        [DataMember(IsRequired = true)]
        public string LineID { get; set; }
        [DataMember]
        public double CutLength { get; set; }
        [DataMember]
        public string Comment { get; set; }
        [DataMember]
        public string GeoName { get; set; }
        [DataMember]
        public bool IsClosed { get; set; }
        [DataMember(IsRequired = true)]
        public string Phases { get; set; }
}

Feel free to let me know if the problem still exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM