Question

I'm trying to make an api call using NSURL Request to upload an image and I continue to get 500 error. To show you what I'm trying to accomplish here is what the curl command would look like.

$ curl -i -F api_password=<YOUR_API_PASSWORD> -F file=@<LOCAL_FILE_PATH> https://upload.api.com/

Here is what I have so far and I continue to get 500 errors. Can you guys give me any direction on what I'm doing wrong (I'm no NSURLRequest master).

NSString *apiPass = @"apiKey";
NSString *filePath = imagePath;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://upload.wistia.com"]];
[request setHTTPMethod:@"POST"];

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

// Create data string
NSString *dataString = [NSString stringWithFormat:@"api_password=%@&file=@%@", apiPass, filePath];

NSData *postData = [dataString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

[request setHTTPBody:[dataString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse * response = nil;
NSError * error = nil;

[NSURLConnection sendSynchronousRequest:request
                      returningResponse:&response
                                  error:&error];
Was it helpful?

Solution 2

When I want to upload "photo.jpg" to some server, I use code like this:

UIImage *image = [UIImage imageNamed:@"photo.jpg"];

NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"%@", endItemBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

In this case data representation of image will be iploaded instead of link to image. You can try someting like this.

OTHER TIPS

Try to compose your POST-request similar this:

NSString *boundary = @"---###-----##----##--#----###---BOUNDARY---###";
NSMutableData *postBody = [NSMutableData data];
NSString *postString = nil;

postString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
postString = [postString stringByAppendingString:@"Content-Disposition: form-data; name=\"api_password\"\r\n\r\n"];
postString = [postString stringByAppendingString:apiPass];

postString = [postString stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n", boundary]];
postString = [postString stringByAppendingString:@"Content-Disposition: form-data; name=\"file\"\r\n\r\n"];
postString = [postString stringByAppendingString:filePath];

postString = [postString stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary]];

[postBody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://upload.wistia.com"] 
                                                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                   timeoutInterval:60.0f];
[request setHTTPMethod:@"POST"];    

[request setValue:@"*/*" forHTTPHeaderField:@"Accept"];   
[request setValue:[@"multipart/form-data; boundary=" stringByAppendingString:boundary] forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", postBody.length] forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:postBody];

Google for "error 500". It's "Internal Server Error". It's an error on the server. Nothing you can do except sending the server guys an email.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top