Error 400 while uploading image from ios - ios

I am very much new to ios.I have to upload an image to rest server from ios application. I have referred stackoverflow. But everytime the response is 400 which is bad request from client side. I am taking image from device document directory.Could some one post the exact code for image upload.Please refer the code I am using. I am not sure what to use as file name since I am downloading image from documents directory.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:#"POST"];
[request setURL:[NSURL URLWithString:#"urltoupload"]];
NSString *stringBoundary = #"----1010101010";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",stringBoundary];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition:attachment; name=\"userfile\"; filename=\"image.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:pngImageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"return Data ---- %#", str);

Try this:
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData: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 URL
[request setURL:requestURL];
Hope it Helps!!

First convert UIImage to NSData as follows
UIImage *image = [UIImage imageNamed:#"example.png"];
NSData *data = UIImagePNGRepresentation(image);
or
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
Then form your request string
NSURL *url = [NSURL URLWithString:serverURL];
//Time out interval need to be tuned
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: [requestString dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (!data)
{
NSLog(#"Error downloading data: %#", error);
return;
}
NSError *error1;
NSDictionary *theDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error1];
DLog(#"Data received :%#", theDict);
}];

Related

Not able to get the multipart image in spring from objective c

I got UIImage after that i converted UIImage to JPEG and now again I'm converting into multipart file then I'm passing to spring rest controller
using post and storing that image into my database. but I'm getting null MultipartFile in spring.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:#"myURL"]];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);// image is UIImage
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"unique-consistent-string";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// 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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#; filename=imageName.jpg\r\n", #"imageFormKey"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData: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"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"Request reply: %#", requestReply);
}] resume];
I'm passing image like above in objective c
#RequestMapping(value = "/livesessionimage", method = RequestMethod.POST)
#ResponseBody ResponseEntity<Response<String>> livesessionImageUpload(#RequestBody MultipartFile file) throws Exception{
System.out.println("-------------"+file.length());// here i'm getting file null
return "";
}

How to post image on server with dictionary iOS?

I have a dictionary (dict) of some keys/values and an image. Now I want to upload image to server with dictionary. I have already try this but not getting any success. Here is my sample code--
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[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"];
NSData *imageData = UIImageJPEGRepresentation(image,0.5);//or you can use png representation- UIImagePNGRepresentation(image);
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"thumbnail\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter all_data
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"all_data\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",dict] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
NSError *error=nil;
NSHTTPURLResponse *response=nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
I need help where is I'm going wrong.. Thanks in advance
Try out the below code:
-(void)uploadImage:(NSString *)api :(NSDictionary *)params : (UIImage *)image {
NSURL *myURL = [NSURL URLWithString:api];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:myURL];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#; charset=UTF-8", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *theBodyData = [NSMutableData data];
for (int i=0; i<[[params allKeys] count]; i++)
{
[theBodyData appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *value=#"";
value=[params objectForKey:[[params allKeys] objectAtIndex:i]];
NSString * value1=[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#\r\n\r\n%#\r\n",[[params allKeys] objectAtIndex:i],value];
[theBodyData appendData:[value1 dataUsingEncoding:NSUTF8StringEncoding]];
}
[theBodyData appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[theBodyData appendData:[#"Content-Disposition: form-data; name=\"my_file1\"; filename=\"image1.jpeg\"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[theBodyData appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
NSData *myData = UIImageJPEGRepresentation(image,0.5);
[theBodyData appendData:[NSData dataWithData:myData]];
[theBodyData appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:theBodyData];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", (int)[theBodyData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody: theBodyData];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask * dataTask =[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Server Raw Response : %#",responseString);
}
}];
[dataTask resume];
}
Your code looks fine but I think you have to convert your dictionary into json string. Just Replace this line-
[body appendData:[[NSString stringWithFormat:#"%#",dict] dataUsingEncoding:NSUTF8StringEncoding]];
with
NSString *jsonStr = [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
[body appendData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
may be this will help you.

How to send form-data and x-www-form-urlencoded both in same POST request in ios?

I have a problem. I am making POST request to server. In which I am uploading an Image with user_id of User.I need to send user_id in x-www-form-urlencoded and Image in from-data. I tried many ways but every time user_id is undefined in server. How can I send both in same request.
Here is my CODE:
NSString *urlString = [[NSString alloc]initWithString:[NSString stringWithFormat:#"MY URL TO UPLOAD IMAGE "]];
urlString=[urlString stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:100];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"------V2ymHFg03ehbqgZCaKO6jy";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
[request addValue:#"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
NSDictionary *params = #{#"user_id":#"213"};
NSMutableData *body = [NSMutableData data];
for (NSString *param in params) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: x-www-form-urlencoded; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add image data
NSData *imageData = UIImageJPEGRepresentation(image, .3);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"my-file\"; filename=\"%#.jpg\"\r\n",#"213"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData: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];
NSURLResponse *urlResponse;
NSError *error;
NSData * data=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
Output: data = nil
In order to send your data through POST
[request setHTTPMethod:#"POST"];
NSString *myString = [NSString stringWithFormat:#"value1=test3&value2=test"];
[request setHTTPBody:[myString dataUsingEncoding:NSUTF8StringEncoding]];

Upload multipart image with NSData in JSON (HTTP body)

I'm trying to upload a multipart image to server where image is converted to NSData and this NSdata is sent a parameter in HTTP body in a JSON string. Unfortunately the app crashes with this message:
**'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteMutableData)'**
I understand that the problem is with JSON. The sample code:
-(void) uploadmultipartimage {
NSString * urlimageupload = [NSString stringWithFormat:#"%#api/mobile_profiles/avatar_upload",URLPrefixCertintell];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlimageupload]];
NSData *imageData = UIImageJPEGRepresentation(_profileimage, 1.0);
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"PUT"];
NSString *boundary = #"unique-consistent-string";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// 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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#; filename=imageName.jpg\r\n", #"imageFormKey"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *jsonString = #{#"user":#{#"user_token":[[NSUserDefaults standardUserDefaults] stringForKey:#"user_token"]},#"api_key":APIKey,#"profile":body};
//***Code Crashes here**//
NSData *postData = [NSJSONSerialization dataWithJSONObject:jsonString options:0 error:nil];
//
// setting the body of the post to the reqeust
[request setHTTPBody:postData];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(data.length > 0) {
//success
}
}];
}
You need to convert your data to NSString:
NSString *g=[[NSUserDefaults standardUserDefaults] stringForKey:#"user_token"];
if (!g)
g=#"Default Token";
NSString *base64Body = [body base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *jsonString = #{#"user":#{#"user_token":g},#"api_key":APIKey,#"profile":base64Body};
Then you can always argue about the possibility of nil when reading the "user_token".
Take care,
/Anders.

Ios multipart request not getting content type at server side

I am doing small project in which I need to send image file to server for that I am using multipart request.
following is my code
UIImage *resizedImage1 = [img resizedImage:CGSizeMake(90.0f, 90.0f) interpolationQuality:kCGInterpolationHigh];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(resizedImage1,0.8)];
NSString *urlString = #"myUrl/accounts/add_profile_image";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
// NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; image/jpeg; boundary=%#",boundary];
NSString *accessToken=[[NSUserDefaults standardUserDefaults]objectForKey:#"userAccesstoken"];
NSLog(#"access token %#",accessToken);
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[request addValue:accessToken forHTTPHeaderField:#"auth"];
NSLog(#"request has %#",request);
// NSLog(#"imagedata has %#",imageData);
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter v=1000
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"v\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"1000\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
// mz_access_token sewt token parameter
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"mz_access_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"%#\r\n",accessToken]dataUsingEncoding:NSUTF8StringEncoding]];
// image data parameter
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"user_image\"; filename=\"image.jpg\"\r\n\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
//[postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
**[postbody appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];**
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"body has %#",postbody);
[request setHTTPBody:postbody];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [postbody length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#", returnString);
NSError *errorReturned = nil;
NSError *error = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
//check netwrok
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&theResponse
error:&errorReturned];
if(data)
{
//parsing json
// again converting into ns dictionary object
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
//NSLog(#"post data %#",postbody);
// NSLog(#"response of web ser %# :\n %#",name,jsonArray);
NSLog(#"Response data %#",jsonArray);
// return jsonArray;
}else{
NSLog(#"error %#",error);
}
// return [[NSDictionary alloc]init];
});
This is my web service request.
I am setting content type like
[postbody appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
but still at server side I am getting #content-type =nill
*At server side we used ROR for webservice*
Please help me to solve this.
thanks in advance..
You should be setting content type by header parameters as
[request setValue:#"image/jpeg" forHTTPHeaderField:#"Content-Type"];
Please make the change.
For every multipart/form element, you need to set a proper Content-Type, e.g. the following for text-parameters:
[postbody appendData:[[NSString stringWithFormat:#"Content-Type: text/plain;charset=utf-8"] dataUsingEncoding:NSUTF8StringEncoding]];

Resources