我想从iOS发布一项活动到Strava。
Strava docs(http://strava.github.io/api/v3/uploads/#post-file)卷曲示例如下:
示例请求
$ curl -X POST https://www.strava.com/api/v3/uploads \
-F access_token=83ebeabdec09f6670863766f792ead24d61fe3f9 \
-F activity_type=ride \
-F file=@test.fit \
-F data_type=fit
在这种情况下,文件test.fit是要发布的活动。
我试图使用AFNetworking异步发布此内容。我有以下测试代码:
NSURL *url = [NSURL URLWithString:@"https://www.strava.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *fileData = [NSData dataWithContentsOfFile:filename];
AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:kStravaTokenStored];
NSString *accessToken = credential.accessToken;
NSDictionary *parameters = @{@"access_token": accessToken, @"activity_type" : @"ride",@"data_type" : @"fit", @"name" : @"Test", @"stationary" : @"1" };
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/api/v3/uploads" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:fileData name:@"Test" fileName:@"Test.fit" mimeType:@"application/octet-stream"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"succss %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure %@ \n\n %@", error, operation);
}];
[httpClient enqueueHTTPRequestOperation:operation];
目前我看到以下错误:
错误域= AFNetworkingErrorDomain代码= -1011“预期状态代码 在(200-299),得到400“UserInfo = 0x9b62300 {NSLocalizedRecoverySuggestion = { “消息”:“坏 请求”, “错误”:[{ “资源”: “上传”, “字段”: “数据”, “代码”: “空”}]},
任何人都知道我在这里缺少什么?
感谢蚂蚁
答案 0 :(得分:2)
必须是
[formData appendPartWithFileData:fileData name:@"file" fileName:@"Test.fit" mimeType:@"application/octet-stream"]
是不是?