6

I have an array of location points (latitude, longitude, and created_at) that need to be sent up in bulk. However, when I use JsonConvert.SerializeObject() it returns a string which can't be parsed on the server endpoint.

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":.

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

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, []>? I haven't found an example that DOESN'T use the <string, string> pair.

Is there another work-around for HttpClient for arrays of json data?

3
  • First, what does the server expect? Commented Mar 30, 2018 at 3:25
  • @Nkosi I updated the post for you. Commented Mar 30, 2018 at 3:30
  • @dbc How would you use PostAsync and just send up the object as is? I've included my Location.cs. Commented Mar 30, 2018 at 3:33

1 Answer 1

7

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'm getting a 401 from the server response, but I'm sure it's something on my end!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.