I’m trying to send a JSON request using AFNetworking and have a problem with making values be translated to the json form of {"value": true}. Instead, I’m getting: {"value": 1}
Here’s basically how I’m creating the request:
NSMutableURLRequest *request =
[self.httpClient requestWithMethod:@"POST"
path:url
parameters:@{@"value": @YES}];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request ...];
[operation start];
Am I missing something trivial here? 🙂
Short answer:
Make sure you are running a recent version of AFNetworking. That’s all I can see as the problem based on the code you’ve provided.
Long answer:
I’ve tried reproducing the issue you’re describing with the most recent versions of AFNetworking and I could not. I dug into AFNetworking to see how the encoding of JSON is done. AFHTTPClient.m:442 uses NSJSONSerialization to encode JSON requests. I came up with the following code to test the issue:
outputs:
So
@YESshould do it. As a note, be sure not to use@(YES)in your code as it will output as a1instead oftrue.outputs:
With that I went through and tried to figure out how AFHTTPClient need to be configured to send out a bool as
1/0instead oftrue/falseand could not find any. Here’s my networking code.