In my project I have a requirement of uploading an image to the server.I have implemented the code below but due to some issues the image is not getting uploaded in the server. I am not getting any response from the server end. The API is working perfectly.I have checked it in postman it is working fine. I am posting the code below.
-(void)callProfilePictureUpdateServiceWithUserID:(NSString *)strUserID ImageData:(NSData*)imgData WithCompletionHandler:(CompletionHandler)handler
{
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:urlService.absoluteURL];
[request setHTTPMethod:@"POST"];
NSMutableData *body=[NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// Set Image Data
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"avtar.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Set User ID.
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strUserID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSOperationQueue *myQueue=[[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
handler(connectionError,YES,@"Connection error happen, please try again later.");
}else{
}
}];
}
Any ideas what could be missing?