objective c - box file upload api from iOS is not working -
i trying upload file box account using box's http api (not ios sdk), reason not working , returning status code 400. mistake in following code?
nsstring *uploadurl = @"https://upload.box.com/api/2.0/files/content"; nsurlsessionconfiguration *config = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession *uploadsession = [nsurlsession sessionwithconfiguration:config delegate:self delegatequeue:nil]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:[nsurl urlwithstring:uploadurl]]; [request sethttpmethod:@"post"]; [request addvalue:[nsstring stringwithformat:@"bearer %@", @"box_access_token_here"] forhttpheaderfield:@"authorization"]; nsstring *boundary = @"some_random_boundary_value"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@", boundary]; [request addvalue:contenttype forhttpheaderfield:@"content-type"]; nsmutabledata *body = [nsmutabledata data]; [body appenddata:[[nsstring stringwithformat:@"--%@\r\n", boundary] datausingencoding:nsutf8stringencoding]]; [body appenddata:[@"content-disposition: form-data; name=\"attributes\"\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [body appenddata:[@"content-type: application/json\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"'{\"name\":\"%@\", \"parent\":{\"id\":\"%@\"}}'", @"file_name", @"box_folder_id"] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n", boundary] datausingencoding:nsutf8stringencoding]]; [body appenddata:[@"content-disposition: form-data; name=\"file\"\r\n" datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"content-type: %@\r\n\r\n", @"mime_type"] datausingencoding:nsutf8stringencoding]]; [body appenddata:uiimagejpegrepresentation(someuiimage, 1.0)]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@--\r\n", boundary] datausingencoding:nsutf8stringencoding]]; [request setvalue:[nsstring stringwithformat:@"%lu", (unsigned long)body.length] forhttpheaderfield:@"content-length"]; [request sethttpbody:body]; nsurlsessionuploadtask *uploadtask = [uploadsession uploadtaskwithrequest:request fromdata:body completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { if (error) { nslog(@"%@", error.debugdescription); } else { nslog(@"%@", @"successfully uploaded"); } }]; [uploadtask resume];
i'm not sure wrong code getting same 400 bad request
when tried implement upload in way it's described here https://box-content.readme.io/#upload-a-file.
i ended replicating behavior android sample code uses - , it's different in api docs:
content-type: multipart/form-data; boundary=dcf6d5da-6b79-4d80-8503-9ca17037e7c3 content-length: 757 --dcf6d5da-6b79-4d80-8503-9ca17037e7c3 content-disposition: form-data; name="parent_id" content-type: text/plain; charset=utf-8 content-length: 10 content-transfer-encoding: binary 3973764013 --dcf6d5da-6b79-4d80-8503-9ca17037e7c3 content-disposition: form-data; name="filename"; filename="your-file-name" content-type: text/html content-length: 300 content-transfer-encoding: binary file binary content goes here --dcf6d5da-6b79-4d80-8503-9ca17037e7c3--
please note how parent_id
, filename
parts used - quite in contrast advices in api docs.
Comments
Post a Comment