简体   繁体   中英

HTTPClient Post Returns Bad Request 400

I've hit a brick wall. Please help. I never hit any breakpoints in my svc file so I don't know whats happening.

I've tried several other's solutions but no dice....

my jsonstring is

string jsonString = "[{\"details\":\"Hello World\"}]";

Rest files.cs:

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "updateDataViaPost", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        ResponseData updateDataViaPost(RequestData rdata);

Client Code:

 using (HttpClient client = new HttpClient())
{
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var request = new StringContent(jsonString, Encoding.UTF8, "application/json");
                var response = client.PostAsync(requestUrl, request).Result;
                var result = response.Content.ReadAsStringAsync();
}

RequestData

public class RequestData
    {
        [DataMember]
        public string details { get; set; }
    }

It now makes it to the function here but rdata which is passed to it is null:

public Stream updateDataViaPost(RequestData rdata)
{
            
     var data = rdata.details.Split('|');

    ......

Looks like the string needed to be string jsonString = "[{"details":"Hello World"}]";

Thanks for everyone's help.!!!!!!! Greatly appreciated.

For future reference for all this works now:

[DataContract]
    public class UpdateTableData
    {
        [DataMember]
        public string TableName { get; set; }
        [DataMember]
        public string ColumnNames { get; set; }
        [DataMember]
        public string Values { get; set; }
        [DataMember]
        public string WhereColumnNames { get; set; }
        [DataMember]
        public string WhereValues { get; set; }
    }
 public string updateDataViaPost(UpdateTableData rdata)
 {
           ..... (MY UNIQUE CODE)......
}
[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "updateDataViaPost", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]

        //ResponseData updateDataViaPost(RequestData rdata);
        string updateDataViaPost(UpdateTableData data);
string jsonString = new JavaScriptSerializer().Serialize(updateTable);
using (HttpClient client = new HttpClient())
            {
                //POST Request

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var request = new StringContent(jsonString, Encoding.UTF8, "application/json");
                
                var response = client.PostAsync(URI, request).Result;
                var result = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                string strresult = result;
            }

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