Reputation: 43
I've tried to send put with body, but in response I've got a
JSON: {
errorr = "BAD json format";
success = 0;
}
I've tried to use the same string(json array) as in my code for Rest Web Service Client and it works fine, but not in my app
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: carID, @"product_id", firstname.text, @"firstname", surname.text, @"lastname", phone.text, @"telephone", email.text, @"email", orderWithUserInfo, @"options", nil];
NSLog(@"JSON: %@", dic);
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONReadingMutableContainers error:nil];
NSLog(@"JSON: %@", jsonData2);
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"34987598743" forHTTPHeaderField:@"X-Oc-Merchant-Id"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
AFHTTPRequestOperation *operationA = [manager PUT:@"http://***/api/rest/addorders" parameters:jsonString success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
//[self showStream:carsList];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
so I don't know where is the problem
Upvotes: 1
Views: 2278
Reputation: 43
I've add manager.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:dic];
and it solved the problem
Upvotes: 1
Reputation: 226
Pass the parameters as NSDictionary and the manager will handle the serialisation.
AFHTTPRequestOperation *operationA = [manager PUT:@"http://***/api/rest/addorders" parameters: dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
}
The method signature for PUT is:
- (AFHTTPRequestOperation *)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure;
So you must pass the NSDictioanry instead of NSString.Check the request serialiser if is initialised.
Upvotes: 0