如何调用,API中包含类Content的POST方法

时间:2018-09-26 07:09:31

标签: c# asp.net-web-api

当我运行客户端代码时,它会给我500错误。由于它仍然是Web api主题的新手,因此我非常感谢您提供帮助,以找出错误所在。

SELECT t2.Bid,t1.id,t1.Status,t2.startdate,t2.endddate,t2.Address
FROM tblbook t1  
INNER JOIN  tblAddress t2 ON t1.id =t2.Bid
WHEREt1.Status = 'Active' 
AND str_to_date(@check_period_start, '%Y-%m-%d') 
       BETWEEN STR_TO_DATE(t2.startdate,'%Y-%m-%d')
            AND STR_TO_DATE(t2.enddate,'%Y-%m-%d') 
AND str_to_date(@check_period_end,, '%Y-%m-%d')  
       BETWEEN STR_TO_DATE(t2.startdate,'%Y-%m-%d')
            AND STR_TO_DATE(t2.enddate,'%Y-%m-%d')

ObjHeader是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; clsAllTalleyHeaderDetail为

clsAllTalleyHeaderDetail ObjHeader = new clsAllTalleyHeaderDetail();

然后是我的WEb API

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

    }

1 个答案:

答案 0 :(得分:1)

1)对于主api,您必须将Content-Type中的application/json标头设置为HttpClient,例如

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

2)对于示例api,您必须以Content-Type格式在邮递员中将application/json标头设置为raw