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!