gpt4 book ai didi

ios - 使用 AFNetworking 异步发布文件并将上传进度上传到 Strava V3 API

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:15 24 4
gpt4 key购买 nike

我想从 iOS 向 Strava 发布事件。

Strava 文档 ( http://strava.github.io/api/v3/uploads/#post-file ) 的 curl 示例如下:

示例请求

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

目前我看到以下错误:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x9b62300 {NSLocalizedRecoverySuggestion={"message":"Bad Request","errors":[{"resource":"Upload","field":"data","code":"empty"}]},

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

谢谢 Ant

最佳答案

必须是

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

还是不行?

关于ios - 使用 AFNetworking 异步发布文件并将上传进度上传到 Strava V3 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21305707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com