1

I am sending JSON data as request and in response I am getting 0 bytes of data with content type "application/JSON;charset=ISO-8859-1". Why I am getting no response? I think it is related to encoding. Please help.

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", [self getBaseURL], @"FileListIOS"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod:@"POST"];


UserProfile *userProfile = [[AppCacheManager manager] getUserProfile];
NSString *personalPath = userProfile.personalPath;
NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:userProfile.personalPath,userProfile.personalPath,userProfile.userType, nil] forKeys:[NSArray arrayWithObjects:@"currentpath",@"rootpath",@"usertype", nil]];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

[request setHTTPBody:data];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if(data) {

    }
}];
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
KishoreThindaak
  • 391
  • 2
  • 4
  • 14
  • Have you checked the value and contents of `connectionError`? – Marcelo Cantos Dec 28 '13 at 06:18
  • yes there is no connection error it is nil and status is 200 – KishoreThindaak Dec 28 '13 at 06:20
  • `data` and `connectionError` should never both be nil. From the [documentation](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURLConnection/sendAsynchronousRequest:queue:completionHandler:): "If the request completes successfully, the data parameter of the handler block contains the resource data, and the error parameter is nil. If the request fails, the data parameter is nil and the error parameter contain information about the failure." – Marcelo Cantos Dec 28 '13 at 06:24
  • Even for a zero-byte response, I would expect `data` to be not nil, but instead a valid `NSData` instance holding zero bytes. – Marcelo Cantos Dec 28 '13 at 06:24
  • Have you tried this : http://stackoverflow.com/questions/4456966/how-to-send-json-data-in-the-http-request-using-nsurlrequest – Chetan Bhalara Dec 28 '13 at 06:25
  • error is nil and data is 0 bytes, I think I should encode the request json data, Please help me to do that – KishoreThindaak Dec 28 '13 at 06:27
  • Have you checked what is coming from server? – Chetan Bhalara Dec 28 '13 at 06:30
  • just header with 200 status code and "application/JSON;charset=ISO-8859-1" as content type – KishoreThindaak Dec 28 '13 at 06:32
  • I am asking about data from server. – Chetan Bhalara Dec 28 '13 at 06:38
  • If `connectionError` and `data` has zero bytes, that means that the request was successfully received, but simply failed to generate an appropriate response. You should (a) confirm the JSON that was sent in the request looks ok, and then (b) focus on your server code, not this Objective-C code. Look at your server logs and make sure everything is ok (sometimes an error in the server code can generate an empty response). If you still can't figure it out, post another question with request JSON that you've created here and the server code. – Rob Dec 28 '13 at 07:35
  • I need to encode the json how can i do it? – KishoreThindaak Dec 28 '13 at 07:39
  • @user2634244, what do you mean "need to encode"? You've created your JSON correctly. This is the right way to make a JSON request. (Whether this particular JSON is precisely what your web service needs, we obviously can't say.) Or are you telling us that your service is not expecting a JSON request, but rather something like a `application/x-www-form-urlencoded` request? – Rob Dec 28 '13 at 13:50
  • @user2634244 Did you get answer? I am getting same error. – python Dec 29 '14 at 11:56

0 Answers0