简体   繁体   English

使用HTTPClient PostAsync发送数组

[英]Send array using HTTPClient PostAsync

I have an array of location points (latitude, longitude, and created_at) that need to be sent up in bulk. 我有一系列需要批量发送的位置点(纬度,经度和created_at)。 However, when I use JsonConvert.SerializeObject() it returns a string which can't be parsed on the server endpoint. 但是,当我使用JsonConvert.SerializeObject()它返回一个无法在服务器端点上解析的字符串。

var location_content = new FormUrlEncodedContent(new[] {
    new KeyValuePair<string, string>("access_token", $"{Settings.AuthToken}"),
    new KeyValuePair<string, string>("coordinates", JsonConvert.SerializeObject(locations))
});

var response = await client.PostAsync(users_url + bulk_locations_url, location_content);

The result looks like the following: 结果如下所示:

{"access_token":"XX","coordinates":"[{\"created_at\":\"2018-03-27T21:36:15.308265\",\"latitude\":XX,\"longitude\":XX},{\"created_at\":\"2018-03-27T22:16:15.894579\",\"latitude\":XX,\"longitude\":XX}]"}

The array of coordinates comes across as one big string, so it looks like :"[{\\"created_at\\": when it should be :[{"created_at": . 坐标数组作为一个大字符串出现,所以它看起来像:"[{\\"created_at\\":它应该是什么时候:[{"created_at":

So, the server is expecting something like this: 所以,服务器期待这样的事情:

{"access_token":"XX","coordinates":[{\"created_at\":\"2018-03-27T21:36:15.308265\",\"latitude\":XX,\"longitude\":XX},{\"created_at\":\"2018-03-27T22:16:15.894579\",\"latitude\":XX,\"longitude\":XX}]}

Location.cs Location.cs

public class Location
{
    public DateTime created_at { get; set; }
    public double latitude { get; set; }
    public double longitude { get; set; }

    [PrimaryKey, AutoIncrement, JsonIgnore]
    public int id { get; set; }

    [JsonIgnore]
    public bool uploaded { get; set; }

    public Location()
    {

    }

    public Location(double lat, double lng)
    {
        latitude = lat;
        longitude = lng;

        uploaded = false;
        created_at = DateTime.UtcNow;

        Settings.Latitude = latitude;
        Settings.Longitude = longitude;
    }

    public Location(Position position) : this(position.Latitude, position.Longitude) {}
}

Is there a way to make the key value pairs a <string, []> ? 有没有办法让键值对成为<string, []> I haven't found an example that DOESN'T use the <string, string> pair. 我还没有找到一个不使用<string, string>对的例子。

Is there another work-around for HttpClient for arrays of json data? HttpClient是否有针对json数据数组的另一种解决方法?

Construct the model and then serialize the entire thing before posting 构建模型,然后在发布之前序列化整个事物

var model = new{
    access_token = Settings.AuthToken,
    coordinates = locations
};
var json = JsonConvert.SerializeObject(model);
var location_content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync(users_url + bulk_locations_url, location_content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 HttpClient.PostAsync 发送 XML 内容? - How to send XML content with HttpClient.PostAsync? 使用 HttpClient PostAsync 发送 C# NameValueCollection 问题 - C# NameValueCollection problem send with HttpClient PostAsync C# HTTPClient PostAsync 不发送数据 - C# HTTPClient PostAsync does not send data 在 Req.Form.Files c# 中使用 function 应用程序在 PostAsync 中使用 HttpClient 发送文件 - send file using HttpClient in PostAsync using function app in req.Form.Files c# 使用Windows.Web.HttpClient PostAsync()将带有字符串参数的POST请求发送到URL - Send POST request with string parameters to an URL using Windows.Web.HttpClient PostAsync() 使用HttpClient和HttpResponseMessage的带有两个字符串的PostAsync - PostAsync with two strings using HttpClient and HttpResponseMessage 如何使用HttpClient发出PostAsync请求以发送/上传CSV文件? - How to make PostAsync request with HttpClient to send/upload CSV file? HttpClient 中的 PostAsync 不向我的 webapi 发送数据 - The PostAsync in HttpClient doesn't send data to my webapi HttpClient 发送 XML PostAsync 格式错误的请求:文件过早结束? - HttpClient send XML PostAsync Malformed request: Premature end of file? 具有PostAsync代码优化的HttpClient - HttpClient with PostAsync code optimisation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM