2

I'm attempting to create a new Chatter FeedItem using the Salesforce REST API. I'm constructing a multi-part request with the following parameters:

{
  "feedElementType": "FeedItem",
  "subjectId": "005o0000000Eg9AAAS",
  "type": "text",
  "text": "Here is the content of my FeedItem post"
}

The first part of the request contains the image data, and the second part contains JSON data about the feed item. Here's the JSON being passed in under the name of json:

{
  "body": {
    "attachment": {
      "attachmentType": "NewFile",
      "title": "file.png"
    },
    "messageSegments": [
      {
        "type": "Text",
        "text": "this is a test text"
      }
    ]
  }
}

When I get a response back, I'm getting an HTTP 400 with the following error from Salesforce:

{
   errorCode = "JSON_PARSER_ERROR";
   message = "Unrecognized field \"attachment\" at [line:-1, column:-1]";
}

I know that this is definitely recognizing the image file (stored under the name of feedElementFileUpload in the multipart request) because I was getting a different error when I encoded the image incorrectly. Does anyone know why I'd be getting this error?

Here's the Objective-C code sending the request using AFNetworking 2.0:

NSArray *message = @[@{@"type": @"Text", @"text": @"this is a test text"}];
NSDictionary *file = @{@"attachmentType": @"NewFile", @"title": @"file.png"};
NSDictionary *body = @{@"body": @{@"messageSegments": message, @"attachment": file}};

NSDictionary *params = @{@"feedElementType": @"FeedItem",
                         @"subjectId": kUserID,
                         @"text": @"example text",
                         @"type": @"text"};

[[AT_WebHelper sharedSalesforceNetworkMgr] POST:kSFEndpointPostToChatter parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    //Add the file if there is one
    if (self.photoTaken.image != nil) {
        NSData *data = UIImagePNGRepresentation(self.photoTaken.image);
        [formData appendPartWithFileData:data name:@"feedElementFileUpload" fileName:@"file.png" mimeType:@"image/png"];
    }

    //Add the attachment metadata
    NSDictionary *headers = @{@"Content-Type": @"application/json; charset=UTF-8", @"Content-Disposition": @"form-data; name=\"json\""};
    [formData appendPartWithHeaders:headers body:STR_DATA([Universal jsonStringForObject:body])];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"Response: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"ERROR: %@", error);
    NSLog(@"RESPONSE: %@", operation.responseObject);
}];

Thanks!

1

In v32.0 and higher of the API, you need to use capabilities instead of attachments. See the example for posting a feed item with existing content to get an idea of what you need to do. For posting a feed item with a new file, there are examples in the section about uploading binary files.

The API reference to look at is the Feed Item Input page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.