I'm using AFNetworking 2.0 for a normal GET request and the parameter dictionary seems to be having no effect. I'm using the standard:
[manager GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {...
but my NSMutableDictionary (parameters) doesn't seem to have any effect on the URL sent in the GET request (GET requests don't have HTTP Bodies as far as I'm aware so they won't be there). So where are the parameters going? I know I could always amend the string, but the way in which I receive the parameters on an optional basis makes it far easier to just send in a dictionary. What's wrong, why isn't it working?
Thanks,
Mike
UPDATE: I'm setting the parameter like this: [parameters setValue:self.Object.ID forKey:#"Filter.Id"]; ID is a string. However, when I do [parameters valueForKey:#"Filter.Id"] I get a (null) in the NSLog. I'm going to mess around with it a little and see how it goes, but if you have any ideas why this is happening I'd appreciate it. Also, parameters is an NSMutableDictionary and is a property of the ViewController.
Was an issue with my NSMutableDictionary property. Changed it to a global variable and everything worked fine!
Thanks,Mike
Related
Let me start by saying I know the problem is the backend is the response, but there is nothing I can do about it. So...
I have a GET request that, on some cases, will send back a 200 response with an empty body. Again, can't change it.
Using Restkit, I have fairly every possible case properly mapped and everything works like a charm, but with this specific case I can't get it to be mapped as it should.
From everything I've seen, this seems to be the most accurate option to what I'm trying to do.
But, for some reason, the response descriptor doesn't seem to be picked up and the response is treated as a failure with the following error:
Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'"
Is there an 'easy' way to properly map an empty response, or even a string as response (clearly with no JSON structure whatsoever) ?
Thanks in advance for any light you can shed in my direction.
As described in this post, there is an even better solution to your problem.
You can simply add an ResponseDescriptor with an ObjectMapping to NSNull:
// Create RKObjectMapping for NSNull:
RKObjectMapping* responseMapping = [RKObjectMapping mappingForClass:[NSNull class]];
// Create RKResponseDescriptor with mapping:
RKResponseDescriptor* response = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodAny
pathPattern:#"/yourPathPattern"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// Add RKResponseDescriptor to RKObjectManager:
[[RKObjectManager sharedManager] addResponseDescriptor:response];
You should not have 2 response descriptors in this case, you should only have one. That one response descriptor is linked to a dynamic mapping which analyses the response and returns the appropriate mapping (from your 2 existing mappings).
I'm creating a module that receive and REST URL and need to match a pattern and extract the parameters
For example:
the URL "http://Product/1" should match the pattern "http://Product/{productId:long}"
and return the Dictionary with productId as a key and "1" as the value in as long
Does anyone knows about a Framework for IPhone that does it, or at least some of it?
NSURL has a method pathComponents, which returns an array with all the different path components. That should help you get the integer part. To get the name I'd use the host method of the NSURL. The docs say, that it should work if the URL is properly formatted, might as well give it a try then.
All in all, no need to convert into a string, there seems to be plenty of methods to work out the components of the URL from the NSURL object itself.
NSString *path = [[#"path+with+spaces"
stringByReplacingOccurrencesOfString:#"+" withString:#" "]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
How can I make a request that contains no headers fields? The requests are being sent to my own server implementation from scratch, which doesn't care about header fields. The request will at most contain only a post body. Let me know if I'm missing something logical.
Please don't tell me about ASIHTTPRequest. Thank you.
As I wanted to remove specific header on a NSMutableURLRequest, I've just found that calling setValue:forHTTPHeaderField: with a nil value actually removes it.
It's not documented by Apple, but it seems quite logical.
For me the code
[request addValue:#"" forHTTPHeaderField:#"User-Agent"];
only added an empty string instead of cleaning the User-Agent.
Instead, using setValue fixed it:
[request setValue:#"" forHTTPHeaderField:#"User-Agent"];
I found that for some of the header fields ("User-Agent" is one of them), setting the header value to nil using
[request addValue:nil forHTTPHeaderField:#"User-Agent"];
doesn't actually remove the header field, but rather sets it to a default value!
If you want to actually remove the content, it is enough setting the value to an empty string with
[request addValue:#"" forHTTPHeaderField:#"User-Agent"];
Why not just ignore them, if you control the server implementation?
Does [request setAllHTTPHeaderFields:[NSDictionary dictionary]] work?
If #2 didn't work, try making your own subclass that always returns an empty dictionary from the allHTTPHeaderFields method, and nil from the valueForHTTPHeaderField: method. But NSURLConnection might make a copy of your request, so you might have to override copyWithZone: also.
I'm pretty new to iPhone. I want to send a request to a server with a float value for a specific key. Can anyone help me out here how to send a float value using ASIHTTPRequest in the request body. I tried changing the value to NSNumber and send it in body but the server response was an error invalid value type.
[_request setPostValue:[NSNumber numberWithFloat:y] forKey:#"Y"];
Also, I can't send float directly as an NSObject.
you can convert your float value to nsstring and then send that value.
NSString *string=[NSString stringWithFormat:#"%f",y];
[_request setPostValue:string forKey:#"Y"];
This completely depends on your server, and you haven't described that to us at all.
I can guess that the server is probably expecting a string, not an NSNumber. Try changing the code to something more like this:
[_request setPostValue:[[NSNumber numberWithFloat:y] description] forKey:#"Y"];
This might print out the value as a string. (I haven't tested it.) If not, you'll need to use NSNumberFormatter.
An API like http://www.yourWebService.com/PIP.jsp?request=aaa¶m=2
Lets consider the above API gives an JSON value. I use the below coding to get that JSON:
NSURL *url =[NSURL URLWithString:#"http://www.yourWebService.com/PIP.jsp?request=aaa¶m=2"];
NSString *resultedString=[[NSString alloc] initWithContentsOfURL:url];
It returns the JSON to the resultedString variable. It is somewhat small amount of Data.
If that API has large amount of data like in 100's of KB's then whether this coding will work fine, or It will crash?
Many APIs like this generally limit the responses - for example, the twitter API will not return more than 1500 results.
http://dev.twitter.com/doc/get/search
If you are working with JSON responses from web server you can use JSON parser to get the results in the form of NSDictionary.
Better you should go with ASIHTTPRequest class which wrapper around NSUrlRequest , following links will be useful
http://allseeing-i.com/ASIHTTPRequest/How-to-use
http://allseeing-i.com/ASIHTTPRequest-CFNetwork-wrapper-for-HTTP-requests