HTTP 500 for create_all.json

I’m calling the API function POST /users/u/goals/g/datapoints/create_all.json and I’m getting a 500 Error

{ URL: https://www.beeminder.com/api/v1/users/me/goals/prayrosary/datapoints/create_all.json }, response: <NSHTTPURLResponse: 0x17e6afe0> { URL: https://www.beeminder.com/api/v1/users/me/goals/prayrosary/datapoints/create_all.json } { status code: 500, headers {
    "Cache-Control" = "no-cache";
    Connection = "keep-alive";
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Tue, 03 Mar 2015 15:35:25 GMT";
    Server = "nginx/1.4.7 + Phusion Passenger 4.0.41";
    "Set-Cookie" = "_beeminder_session=BAh7Bzoeb3JpZ2luYWxfb3JpZ2luYWxfcmVmZXJlckkiBi8GOgZFRkkiD3Nlc3Npb25faWQGOwZGIiVlNmMwZTBiMWI4NTU1ZDE1OWU1NDUzZTFhMjVkZTc4NQ%3D%3D--e32342f10f69f70212c74ccbe395e1353df14b9d; path=/; HttpOnly";
    Status = "500 Internal Server Error";
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = "Phusion Passenger 4.0.41";
    "X-Runtime" = "0.396489";
    "X-UA-Compatible" = "IE=Edge,chrome=1";
} }> - Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 500" UserInfo=0x17deb220 {NSLocalizedRecoverySuggestion={"error":"Internal Server Error"},
2 Likes

The problem here is that you were passing in a proper form/urlencoded array of datapoints, while we were expecting a json-encoded string representation of the same. Which is kind of weird, and poorly documented [1], so shame on us!

The trouble here is deep… we return json data through the api, but we don’t really accept json data posted to the api – it’s all x-www-form-urlencoded data.

If you want to pass in a thing like an array of arrays (roadall), or an array of datapoint parameters (create_all), we were asking you to construct your array, then turn it into a json-parsable string, pass us that, then we parse the string as json on our end.

This is admittedly surprising / confusing (perhaps we should make it an actual json api for v2). You can send nested lists and things with x-www-form-urlencoded data as well, but it’s kinda ugly [2] if you’re trying to curl things, for example. Of course if you have a nice civilized library doing the dirty work on your http calls it’s beneath you to worry about such things anyway. So what I just did makes it so you can submit form-data-url-encoded arrays, or you can submit the json-encoded string version.

This should work for both roadall and datapoints#create_all in the api now.

[1] I think this used to be better documented and we may have murked it up in the past several months. I believe this because I know that people have successfully used the datapoints#create_all endpoint in the past. Sorry!

[2] this example is borrowed from HTTParty

@example
  # { name: "Bob",
  #     address: {
  #       street: '111 Ruby Ave.',
  #       city: 'Ruby Central',
  #       phones: ['111-111-1111', '222-222-2222']
  #     }
  #   }.to_params
  # => "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
2 Likes

Whoops, edited my original answer because I was wrong. If you get these by email you might want to check out the updated answer in-forum.

Cheers!
B

1 Like

It seems to be working now.

2 Likes