What causes this error message? The remote server

2019-08-12 06:57发布

问题:

I try to submit a request to a REST API using WCF; here's what I've done:

namespace Sample
{
    [ServiceContract]
    [XmlSerializerFormat]
    public interface ISampleApi
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "users.xml", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
        User CreateUser(User user);
    }
}

And this is my User Class:

namespace Sample.Entity
{
    [XmlRoot("user")]
    public class User
    {
        [XmlElement("company")]
        public string Company { get; set; }

        [XmlElement("country-code")]
        public string ContryCode { get; set; }

        [XmlElement("created-at")]
        public DateTime CreatedAt { get; set; }

        [XmlElement("email")]
        public string Email { get; set; }

        [XmlElement("external-identifier")]
        public string ExternalIdentifier { get; set; }

        [XmlElement("id")]
        public int Id { get; set; }

        [XmlElement("measurement-system")]
        public string MeasurmentSystem { get; set; }

        [XmlElement("profile")]
        public string Profile { get; set; }

        [XmlElement("url")]
        public string Url { get; set; }

        [XmlElement("username")]
        public string Username { get; set; }

        [XmlElement("account-type")]
        public string AccountType { get; set; }
    }
}

But when I call CreateUser method and pass a User object to it I receive this error message:

The remote server returned an error: (422) Unprocessable Entity.

Any idea what causes this?

回答1:

That exception means that the web server responded with an error code, namely 422. You will need to check with the administrator of the remote site, why that might be. (Or look at the body of the response if any was returned, it might include some hints).

Here is the explanation of error code 422: http://tools.ietf.org/html/rfc4918#section-11.2

The request you are sending to the server is most likely invalid in some way or another. What the exact error might be, is impossible to tell without knowing which request you are sending against which system.



回答2:

This error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous XML instructions

Re-check the users.xml for instructions
country-code is string or integer value?