Reputation: 3083
I am trying to post the data on the server which ask for Json format. here's the requirement of json format that needs to be send to the server
{"storeId":"[email protected]",
"handleName":"abcd",
"profilePic":"(encoded bse64image in string)"
}
and response I will get is in json format
Here's what i am doing
- (void)postTo:(NSString *)urlString params:(NSDictionary *)params completion:(void (^)(id, NSError *))completion {
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
if (params.count) {
NSMutableArray *paramArray = [NSMutableArray array];
for (NSString *key in [params allKeys]) {
NSString *encodedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedValue = [params[key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedPair = [[encodedKey stringByAppendingString:@"="] stringByAppendingString:encodedValue];
[paramArray addObject:encodedPair];
}
NSString *paramString = [paramArray componentsJoinedByString:@"&"];
NSData *data = [paramString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Accept"];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (!error && httpResponse.statusCode < 400) {
NSError *parseError;
id parse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&parseError];
if (!parseError) {
completion(parse, nil);
} else {
NSLog(@"parse error %@", parseError);
completion(nil, parseError);
}
} else {
NSLog(@"http status %d, error %@", httpResponse.statusCode, error);
completion(nil, error);
}
}];
and calling it like this
NSString *storeId=@"[email protected]";
NSString *handleName = @"abc";
// NSString *profilePic=@"samplestringtest";
UIImageView *image= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"abc.jpg"] ];
_profilepicImageView.image= image.image;
NSData *imag64 = UIImageJPEGRepresentation(_profilepicImageView.image, 0.5f);
NSString *encoded = [imag64 base64EncodedStringWithOptions:0];
NSString *urlString = @"http://server.cloud.com/user/registration";
NSDictionary *params = @{@"storeId":storeId,
@"handleName":handleName,
@"profilePic":encoded
};
[self postTo:urlString params:params completion:^(id result, NSError *error) {
if (!error && result != nil) {
NSLog(@"%@", result);
HUD.progress = 1.0f;
[HUD hide:YES];
[self afterSignupProceed];
} else {
NSLog(@"%@", error);
}
}];
I am getting http status 400 , error null
Upvotes: 0
Views: 1976
Reputation: 8638
This is hard to answer with the information you have given. But I can see the following that might help you.
Firstly, a 400
response code is a server code. It does mean that there was no connection error, so it is expected that the NSError
value is nil
. 400
is the value for Bad Request
, so it is likely that the server dislikes what you have sent. E.g. a parameter is missing.
Secondly, you seem to be sending the data html-form encoded and then telling the server that it is json-encoded. This could be the source of the 400
error from the server. Maybe it only supports json, and the data you sent is not correct json.
Without further information about the format the server is expecting I cannot help you more.
Upvotes: 3