Question

I am making http post from iOS to my web service which was written on .NET platform.

I am using JSONModel to serialize/deserialize and AFNetworking to make http requests.

I can make request and get response successfully. But when any variable on response data is null problem occurs.

Here is how I call my web service:

NSDictionary *parameters = @{@"Username":username,@"Password":password};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"<my_url>" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Request succeeded.
    LoginResponse *response =[[LoginResponse alloc]initWithDictionary:responseObject error:nil];
    // !!! "response" is null here because of Message on response data is null.
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // Request failed.
}];

Here is my LoginResponse.h file:

@interface LoginResponse : JSONModel

@property (nonatomic, strong) NSString *IsSuccess;
@property (nonatomic, strong) NSString *Message;

@end

This is what web service returns when I request via Fiddler:

{"IsSuccess":true,"Message":null}

Since I have null values on response data, deserialization does not work. When I remove (because Message on response data is null) @property (nonatomic, strong) NSString *Message; line from LoginResponse.h file, deserialization works fine.

I don't care whether response data has null variable or not, I just want to deserialize it. I can't estimate what the problem is. Why deserialization does not work when response has null variable?

Any help would be appreciated.

Was it helpful?

Solution

Declare Message optional in LoginResponse:

@property (nonatomic, strong) NSString<Optional>* Message;

See the JSONModel docs for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top