如何调用,在API POST方法,其中包括一类内容(How to Call,the POST met

2019-09-27 11:44发布

当我运行它给了我一个500 error.Since上午还是新的网页API主题的客户端代码,我真的很感激的帮助,以确定哪些是我的失误。

using (HttpClient client = new HttpClient())
{
    ObjHeader.listRtnTalleySheetHeader = lstRtnTalleySheetHeader;
    ObjHeader.listRtnTalleySheetDetail = lstRtnTalleySheetDetail;
    client.BaseAddress = new Uri("http://---service---");
    var url = "api/config/InsertTalleydetail/";
    var alldetails = Newtonsoft.Json.JsonConvert.SerializeObject(ObjHeader);
    HttpContent content = new StringContent(alldetails, Encoding.UTF8, "application/json");
    HttpResponseMessage response = await client.PostAsync(url, content);
    if (response.IsSuccessStatusCode)
        val = "Ok";
    else
        val = "No";
}
return val;

其中ObjHeader是clsAllTalleyHeaderDetail ObjHeader = new clsAllTalleyHeaderDetail(); clsAllTalleyHeaderDetail是

public class clsAllTalleyHeaderDetail
    {
        public ObservableCollection<clsTalleySheetHeader> listRtnTalleySheetHeader { get; set; }
        public ObservableCollection<clsTalleySheetDetail> listRtnTalleySheetDetail { get; set; }

    }

然后我的Web API

 [HttpPost]
        public HttpResponseMessage InsertTalleydetail([FromBody] clsAllTalleyHeaderDetail obj)
         {
                return   todoService.InsertTalleydetail(obj.listRtnTalleySheetHeader , obj.listRtnTalleySheetDetail );
        }

Answer 1:

1)你的主要的API,你必须设置Content-Typeapplication/jsonHttpClient

client.DefaultRequestHeaders.Add("Content-Type","application/json");

2)为您的样品API,您必须设置Content-Typeapplication/json的邮递员与raw格式。



文章来源: How to Call,the POST method in API which includes a class Content