I am trying to upload some data to web service using post action
But the following error happens on didReceiveData.
400 Bad Request
a 404 Not Found
Can you please help me on this coding issue?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
NSMutableData *postData = [[NSMutableData alloc] init];
NSString *boundary = [NSString stringWithString:@"------------------147378098Aadfsn1987XdaoTwq"];
// First name
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"firstname\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[firstName dataUsingEncoding:NSUTF8StringEncoding]];
NSData *photo = UIImageJPEGRepresentation(takePhoto, 0.5f);
// Photo data
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"picture1\"; filename=\"photo.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
if (takePhoto != nil) {
[postData appendData:photo];
}
NSString *postLength = [NSString stringWithFormat:@"%d", postData.length];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Try this Code For Uploading Photo to Server From iPhone.