我正在尝试发送消息和图表文件,但无济于事。此代码适用于发送常规消息。我正在使用AFNetworking来做到这一点。 我使用的stocktwits端点:
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];
我收到的错误消息:
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"}]}
我一直在玩AFNetworking提供的其他解决方案,作为AFMultipartFormData,但也无济于事。
有人知道我在这里缺少什么吗?
谢谢!
答案 0 :(得分:0)
我可以上传一张图表/图片,使用此代码的 API 限制:-
- (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);
}];
}