Pregunta

my URL is correct, but my request must be malformed. I'm getting a 400. Any ideas? The API documentation says it could return a 400 if parameters were passed instead of JSON body. Thanks in advance.

- (void)postFeedItem:(NSDictionary *)paramDict Response:(void (^)(id))callbackBlock Failure:(void (^)())failure {

NSString *targetUrl = [NSString stringWithFormat:@"%@/services/data/v23.0/chatter/feeds/user-profile/%@/", _appManager.coordinator.credentials.instanceUrl, [_appManager.userInformation objectForKey:@"sfUserId"]];

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:targetUrl]];
/*
 { "body" :
 {
 Request body example:
 "messageSegments" : [
 {
 "type": "Text",
 "text" : "New post"
 }
 ]
 }
 }
 */

NSArray *arr = [NSArray arrayWithObjects:
                [NSDictionary dictionaryWithObjectsAndKeys:@"Text", @"type",
                 @"New post", @"text",
                 nil],
                nil];
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSDictionary dictionaryWithObjectsAndKeys:arr, @"messageSegments", nil],
                      @"body",
                      nil];

NSLog(@"JSON: %@", [[NSString alloc] initWithData:[self toJSON:info] encoding:NSUTF8StringEncoding]);

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"feed-items" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendData:[self toJSON:info]];
}];

[request setValue:[NSString stringWithFormat:@"OAuth %@",_appManager.coordinator.credentials.accessToken] forHTTPHeaderField:@"Authorization"];
[request addValue:@"false" forHTTPHeaderField:@"X-Chatter-Entity-Encoding"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"success");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"FAILED CHATTER Request: %@ - %@ - %@", [request URL], [request allHTTPHeaderFields], error.description);
}];

[operation start];

}

Here's the NSLog:

2012-09-19 18:10:54.732 RingDNA Free[6018:c07] __47-[ChatterHelper postFeedItem:Response:Failure:]_block_invoke_087 [Line 76] FAILED CHATTER Request: https://na4.salesforce.com/services/data/v23.0/chatter/feeds/user-profile/00560000001j3paAAA/feed-items - { "Accept-Encoding" = gzip; "Accept-Language" = "en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8"; Authorization = "OAuth 00D60000000KV29!ARkAQFWhnhOtcGFgVMT4MkZHCV3zG9SY4en66718BiG_ZY59W0gR1iSWA8i.ey_b94vqjRW_RQITALBWmfpPrKTGk"; "Content-Type" = "multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY,application/json"; "User-Agent" = "com.ringdna.dreamforce.RingDNA-Free/36 (unknown, iPhone OS 5.1, iPad Simulator, Scale/1.000000)"; "X-Chatter-Entity-Encoding" = false; } - Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code [number of indexes: 100 (in 1 ranges), indexes: (200-299)], got 400" UserInfo=0xa2a8b40 {NSErrorFailingURLKey=https://na4.salesforce.com/services/data/v23.0/chatter/feeds/user-profile/00560000001j3paAAA/feed-items, NSLocalizedDescription=Expected status code [number of indexes: 100 (in 1 ranges), indexes: (200-299)], got 400}

¿Fue útil?

Solución

We figured this out after a few attempts. Basically SFDC validates the order of the parameters you're sending to the Chatter APIs, it appears. Code samples for working Chatter posts are in Github now if this helps anyone else:

https://github.com/kyleroche/Dreamforce-2012

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top