I want to upload image into server. I am getting 502 error while uploading into server. Following is the error I am getting
{ status code: 502, headers {
Connection = "keep-alive";
"Content-Length" = 181;
"Content-Type" = "text/html";
Date = "Mon, 09 Oct 2017 10:47:49 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
} }
In the error Content-Type showing text/html but I placed it as multipart/form-data.
If I remove the boundary in Content-Type my server throws 105 error, backend developer said 105 means content-type is not matching.
below is my code:
-(void)uploadImage:(UIImage *)image withBlock:(void (^)(NSError *error))block {
self.apiPath = postProfilePath;
NSString *requestUrl = [NSString stringWithFormat:@"%@%@", self.baseUrl, self.apiPath];
requestUrl = [requestUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [self getRequestFor:requestUrl withImage:image cookies:[[AppManager sharedManager] storedCookies]];
self.dataTask = [[AdmeNetworkManager sharedNetworkManager] requestWithRequestObject:request completionHandler:^(NSURLResponse *response, id responseData, NSError *error) {
if(error) {
NSLog(@"error while uploading image %@",error);
if(block) {
block(error);
}
} else {
NSDictionary *responseDict = (NSDictionary *)[AdmeUtility parseJsonData:responseData];
NSLog(@"success %@",responseDict);
if ([AdmeNetworkError isSuccessResponse:responseDict]) {
if(block) {
block(error);
}
} else {
if(block) {
NSLog(@"error upload Image %@",error);
error = [NSError errorWithDomain:@"adme" code:-3003 userInfo:responseDict];
block(error);
}
}
}
}];
}
-(NSMutableURLRequest *)getRequestFor:(NSString *)requestUrl withImage:(UIImage *)image cookies:(NSDictionary *)cookies{
NSError *error = nil;
// Request serializer
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:&error];
if (cookies) {
NSString *token = [NSString stringWithFormat:@"Bearer %@",[cookies objectForKey:@"token"]];
NSString *userId = [NSString stringWithFormat:@"%@",[cookies objectForKey:@"id"]];
[request setValue:token forHTTPHeaderField:@"token"];
[request setValue:userId forHTTPHeaderField:@"appuserid"];
NSLog(@"headers for image %@ %@",token, userId);
}
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
//[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
// post body
NSMutableData *body = [NSMutableData data];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
// NSString *fileNameStr = [NSString stringWithFormat:@"%@.jpg", imageName];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", @"imageCaption"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", @"Some Caption"] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:imageData];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
return request;
}
502 = Bad Gateway error. Your nginx.conf is using one or possibly more proxy_pass directives to talk to a backend, and that backend is offline or not listening on the same address/port specified by the proxy_pass directive.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.