Nested Parameters?
Closed this issue · 2 comments
I have the following code:
NSMutableDictionary *fields = [NSMutableDictionary dictionary];
[fields setValue:username forKey:@"username"];
[fields setValue:location forKey:@"location"];
NSDictionary *params = [NSDictionary dictionaryWithObject:fields forKey:@"student"];
[_client POST:url parameters:params completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
...
}];
Which if you were to print the description, would look something like this:
{
student: {
username: foo;
location: bar;
}
}
And I was noticing that it wasn't attaching any parameters to my request. In investigating the source, I see this inside addParametersToRequest:
of SVHTTPRequest.m
during the enumeration:
if([obj isKindOfClass:[NSString class]]) {
...
}
else if([obj isKindOfClass:[NSNumber class]]) {
...
}
else if([obj isKindOfClass:[NSData class]]) {
...
}
Which doesn't seem to account for recursing into dictionaries. Am I missing something? Should I be using a different technique? I can attempt to fix it and send a pull request if you like.
I also tried sendParametersAsJSON = YES
, but since I can't set the content-type header, my server doesn't know to parse out the JSON and I get garbage data.
Aside from this hiccup, I love this library. MUCH simpler than AFNetworking/RestKit, so thank you.
I was able to fix this by adding this to the start
method of SVHTTPRequest
[self.operationRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
It seems to me that if sendParametersAsJSON = YES
then this header should always be set accordingly, no?
@pwightman excellent point. Yes, Content-Type
should automatically get set to application/json
when sendParametersAsJSON
is set to YES
. Feel free to submit a pull request that fixes this, otherwise I should be pushing a fix in the next day or 2.
Glad to hear SVHTTPRequest is so simple to use, thanks :)