本文介绍了使用AFNetworking异步上传文件到Strava V3 API的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将活动从iOS发布到Strava。

I would like to post an activity to Strava from iOS.

Strava docs()的卷曲示例如下:

Strava docs (http://strava.github.io/api/v3/uploads/#post-file) have curl example as following:

示例请求

$ curl -X POST https://www.strava.com/api/v3/uploads \
    -F access_token=83ebeabdec09f6670863766f792ead24d61fe3f9 \
    -F activity_type=ride \
    -F [email protected] \
    -F data_type=fit

在这种情况下,文件test.fit是要发布的活动。

In this case the file test.fit is the activity to post.

我尝试使用AFNetworking异步发布。我有以下测试代码:

I am attempting to post this asynchronously using AFNetworking. I have the following test code:

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];

当前,我看到以下错误:

Currently I am seeing the following error:

错误域= AFNetworkingErrorDomain代码= -1011预期的状态代码
在(200-299)中,得到400 UserInfo = 0x9b62300
{NSLocalizedRecoverySuggestion = { message: Bad
Request,错误:[{资源:上传,字段:数据,代码:空}]}},

有人知道我在这里缺少什么吗?

Anyone have an idea what I am missing here?

感谢蚂蚁

推荐答案

必须是

[formData appendPartWithFileData:fileData name:@"file" fileName:@"Test.fit" mimeType:@"application/octet-stream"]

还是不?

这篇关于使用AFNetworking异步上传文件到Strava V3 API的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 06:04