0

I'm trying to implement a method in my iOS app to mark an article as read using the Feedly API. However, I keep getting error responses:

{
    "errorCode": 400,
    "errorId": "sandbox-he.2015010906.2770040",
    "errorMessage": "no data"
}

This is the method:

- (void)markRead:(NSString*)articleID {
    NSLog(@"Artidle ID is: %@", articleID);

    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *accessToken = [standardUserDefaults objectForKey:@"AccessToken"];

    NSString *feedUrl = [NSURL URLWithString:@"https://sandbox.feedly.com/v3/markers"];

    NSError *error = nil;

    NSString *post =[[NSString alloc] initWithFormat:@"action=markAsRead&type=entries&entryIds=%@",articleID];
    NSLog(@"PostData: %@",post);

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:feedUrl];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
    [request setHTTPBody:postData];
    [request addValue:accessToken forHTTPHeaderField:@"Authorization"];

    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

    NSError *errror = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&errror];

    NSLog(@"Response code: %ld", (long)[response statusCode]);

    if ([response statusCode] >= 200 && [response statusCode] < 300)
    {
        NSLog(@"Success marking this as read.");

    } else {
        if (error) NSLog(@"Error: %@", errror);
        NSLog(@"No success marking this as read.");
    }

}

The Feedly API describes that it requires this as input:

{
   "entryIds": [
       "TSxGHgRh4oAiHxRU9TgPrpYvYVBPjipkmUVSHGYCTY0=_14499073085:c034:d32dab1f",
       "TSxGHgRh4oAiHxRU9TgPrpYvYVBPjipkmUVSHGYCTY0=_1449255d60a:22c3491:9c6d71ab"
   ],
   "action": "markAsRead",
   "type": "entries"
}

In my opinion I'm providing this as input, so I actually expected a proper response. However, I keep getting 400 and can't seem to figure out what I'm doing wrong. I've been busting my head for hours on this.

user4334509
  • 125
  • 2
  • 10
  • 1
    It expects a JSON object since you are using POST. However, your data seems to be formatted for a GET call. – Daniel Jan 09 '15 at 14:57
  • Are you *actually* providing that input in json format, or is it just your *opinion*? It looks a lot like you reformatted your post data as x-www-form-urlencoded instead... – Ian MacDonald Jan 09 '15 at 15:00
  • It's just my opinion. I don't know how to fix this though. – user4334509 Jan 09 '15 at 15:07

1 Answers1

1

Instead of using your current body data, try creating the data with following code:

NSDictionary *bodyDict = [NSDictionary dictionaryWithObjectsAndKeys:@[articleID],@"entryIds",
                          @"markAsRead",@"action",
                          @"entries",@"type", nil];

NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:bodyDict options:0 error:&error];
[request setHTTPBody:postData];

This will send the body as a JSON object.

Sending parameter like you did (with action=markAsRead&type=entries&entryIds=%@) is usually used when calling a web method with the GET request method. Take a look at POST vs GET for a better understanding.

Daniel
  • 20,420
  • 10
  • 92
  • 149