ios - Uploading GPX file to Strava API with GTMHTTPFetcher POST in Objective-c -
after exploring use of strava api (http://strava.github.io/api/v3) gtmhttpfetcher 2 days stuck @ uploading .gpx file account made decision ask answer here. received , stored access token necessary 'view_private,write' permission , server responding requests , managed receive , change data stored in account. when trying upload .gpx (xml) file error (bad request):
the operation couldn’t completed. (com.google.httpstatus error 400.)
there don't understand constructing http post request apparently , tried different approaches experimenting values in http header, changed fetch url after '/uploads?' file=... etc. no avail. authentication use gtmoauth2authentication.
my code:
- (void) uploadtostrava { nsstring *filepath = @"somefilepath.gpx"; nsurl *url = [nsurl urlwithstring:@"https://www.strava.com/api/v3/uploads?data_type=gpx"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; [request sethttpmethod:@"post"]; [request sethttpbody:[nsdata datawithcontentsoffile:filepath]]; [self.signedstravaauth authorizerequest:request]; gtmhttpfetcher* mystravafetcher = [gtmhttpfetcher fetcherwithrequest:request]; [mystravafetcher beginfetchwithdelegate:self didfinishselector:@selector(mystravafetcher:finishedwithdata:error:)]; self.currentfetcher = mystravafetcher; } - (void) mystravafetcher:(gtmhttpfetcher *)fetcher finishedwithdata:(nsdata *)data error:(nserror *) error { if (error != nil) { [self handleerrorwithtext:nil]; nslog(@"error %@", [error localizeddescription]); } else { nsstring *info = [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding]; nsdictionary *result = [self.jsonparser objectwithstring:info]; nslog(@"strava response: %@", result); } }
the api docs come following example request in curl:
$ curl -x post https://www.strava.com/api/v3/uploads \ -h "authorization: bearer 83ebeabdec09f6670863766f792ead24d61fe3f9" \ -f activity_type=ride \ -f file=@test.fit \ -f data_type=fit
related question: using afnetworking post file asynchronously upload progress strava v3 api
if receiving http 400 means strava v3 api rejecting request - 400s uploads endpoint include information in response wrong. start examining response body error response. i'm not familiar gtmhttpfetcher, should either able debug , observe response body or can use tool charles proxy debug request , response. try comparing curl
request , request coming objective-c code.
the strava v3 uploads api expects file portion of upload in file
parameter - again, without being familiar gtmhttpfetcher can't sure doing, doesn't seem specifying file data part of multipart/form-data
section called file
.
a long-shot: seems mixing post body , query string parameters, data_type
parameter might not making through. i'd consistent , put params in post body.
Comments
Post a Comment