I would like to post an activity to Strava from iOS.
Strava docs (http://strava.github.io/api/v3/uploads/#post-file) have curl example as following:
EXAMPLE REQUEST
$ 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
In this case the file test.fit is the activity to post.
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:
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"}]},
Anyone have an idea what I am missing here?
Thanks Ants