gpt4 book ai didi

c# - 将对象列表发送到 Web API

转载 作者:行者123 更新时间:2023-11-30 14:31:23 27 4
gpt4 key购买 nike

我无法将对象列表发送到 webapi Controller 。

这是 Controller :

[AcceptVerbs("POST")]
public string syncFoodData(List<intakeSync> str)
{
string apikey = Request.Headers.GetValues("api_key").ToArray()[0];
return "data syncronised";
}

这是类:

public class intakeSync
{
public int memberID { get; set; }
public Double size { get; set; }
public string food { get; set; }
public string token { get; set; }
public string time { get; set; }
public string date { get; set; }
public string nocatch { get; set; }
public string calories { get; set; }
}

str 的值始终为 null。

这是将 httprequest 发送到 webapi 的 web 方法

public static string syncIntakeData(string token, string[]  syncString)
{

JavaScriptSerializer js = new JavaScriptSerializer();
List<intakeSync> str = new List<intakeSync>();
for (int i = 0; i <= syncString.Length - 1; i++)
{

str.Add(js.Deserialize<intakeSync>(syncString[i]));
}
string url = URI + "/api/Food/?str=" +js.Serialize(str);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("api_key", token);
Stream requestStream = request.GetRequestStream();
StreamReader read = new StreamReader(request.GetResponse().GetResponseStream());
string dat = read.ReadToEnd();
read.Close();
request.GetResponse().Close();
return dat;
}

最佳答案

你可以使用这个:

Json 中的请求主体

[{id:1, nombre:"kres"},
{id:2, nombre:"cruz"}]

API 休息 .net C#

public string myFunction(IEnumerable<EntitySomething> myObj)
{
//...
return "response";
}

关于c# - 将对象列表发送到 Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20348409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com