mikipisang
mikipisang

Reputation: 31

Can not create stocktwits message with a chart

I am trying to send a message along with a chart file, but to no avail. This code works well for sending regular message. I am using AFNetworking to do this. The stocktwits endpoint i used:

https://api.stocktwits.com/api/2/messages/create.json

NSURL *url = [NSURL fileURLWithPath:chartFilePath];
NSDictionary *params = @{@"in_reply_to_message_id": inReplyToMsgId, @"chart":url, @"body":msg, @"access_token":oauthtoken};

NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:reqURL parameters:params];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setResponseSerializer:[AFHTTPResponseSerializer alloc]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{

    NSString *json_string = [[NSString alloc] initWithData:responseObject 
    encoding:NSUTF8StringEncoding];

    NSLog(@"stocktwits: %@", json_string);     
}failure:^(AFHTTPRequestOperation *operation, NSError *error){

     NSLog(@"Failure: %@", error);
     failure(error);
}];
[manager.operationQueue addOperation:operation];

The error message i got:

 stocktwits: {"response":{"status":422},"errors":[{"message":"We couldn't recognize the image format. Format must be one of: image/jpeg image/pjpeg image/png image/x-png image/gif"}]}

I have been playing around with other solutions AFNetworking provides sucha as AFMultipartFormData, but also to no avail.

Does someone know what i am missing here?

Thanks!!!

Upvotes: 3

Views: 172

Answers (1)

Ilam
Ilam

Reputation: 87

I can upload one chart/image, limitation of the API using this code:-

- (void) commonPostWithPath:(NSString*)path params:(NSDictionary*)parameters image:(UIImage*_Nullable)image completion:(MOTwitterCompletionBlock _Nullable ) completion{
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:StockTwits_BaseURL]];
    [manager POST:path parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if (image) {
            [formData appendPartWithFileData:UIImagePNGRepresentation(image) name:@"chart" fileName:@"Stocks Live" mimeType:@"image/png"];
        }
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (completion) completion(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completion(nil, error);
    }];
}

Upvotes: 0

Related Questions