Jeena
Jeena

Reputation: 2222

How to add comments through the SoundCloud API

I would like to post comments via the SoundCloud API but looking at the documentation it seems impossible, the only method available for /tracks/{id}/comments seems to be GET. When I try to send it a POST I get a HTTP Error: 422, same happens if I use the console I get a 422 too, with "error_message": "Body can't be blank" even so I added body=test as a parameter.

Any idea how you are supposed to add a comments via the API?

I can see that it seems to be possible with for example the ruby SDK:

# create a new timed comment
comment = client.post("/tracks/#{track.id}/comments", :comment => {
  :body => 'This is a timed comment',
  :timestamp => 1500
})

but I am using the Objective-C SDK and this is my code so far (which returns a 422):

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:content forKey:@"body"];
if (timestamp >= 0)
{
    [dict setObject:[NSString stringWithFormat:@"%i", timestamp] forKey:@"timestamp"];
}

NSString *resource = [NSString stringWithFormat:@"tracks/%i/comments", trackId];

[scAPI performMethod:@"POST" onResource:resource withParameters:dict context:delegate userInfo:nil];

Upvotes: 2

Views: 1206

Answers (2)

Michael Groves
Michael Groves

Reputation: 1

Here is a great music app which allows you to connect favorably with Amazon's latest music, videos, and movies while freelancing other products as well.

Upvotes: -2

Jeena
Jeena

Reputation: 2222

Ok, I was able to find it out on myself, I had a look at the Java version and its examples:

List<NameValuePair> params = new java.util.ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("comment[body]", "This is a test comment"));

HttpResponse response = api.post("tracks/" + TRACK_ID + "/comments", params);

So the strange decision to not only have bodyas the key but comment[body] and without documenting it in the API was what I had problems with. I tried to do it like the ruby version suggested do have a NSDictionary with the key comment and within another one with body and timestamp but that would just cast errors because the SDK didn't expect nested dictionaries. So now the code looks like this:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:content forKey:@"comment[body]"];
if (timestamp >= 0)
{
    [dict setObject:[NSString stringWithFormat:@"%i", timestamp] forKey:@"comment[timestamp]"];
}

NSString *resource = [NSString stringWithFormat:@"tracks/%i/comments", trackId];

[scAPI performMethod:@"POST" onResource:resource withParameters:dict context:delegate userInfo:nil];

Upvotes: 3

Related Questions