Bug in NSURLRequest or AFNetworking? - ios

Not sure if this is a bug or I am missing something, most likely latter. My AFHTTPClient's base url is:
#define kBaseURL #"http://localhost:4567/api/"
self.client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:kBaseURL]];
When I make a request to, for example '/games', it actually sends the request to http://localhost:4567/games ignoring the API part.

The "baseURL" part of "initWithBaseURL:" bit should make it clear that it's only going to work with the scheme + host + port number part.
Once you've created your client, you can add parameters onto it's URL request via techniques like:
NSMutableURLRequest *request =
[self.client requestWithMethod:#"POST" path:#"/api/games" parameters:parameters];

The AFHTTPClient.h file has tons of comments about exactly how to use /s to make sure everything fits together correctly. Check it out on github

Related

Getting an NSURLRequest for use in UIImageView setImageWithURLRequest:placeholderImage:success:failure:

This question assumes AFNetworking 2.0.
I have an AFHTTPRequestOperationManager, and I have a UIImageView. My URL for the image in question requires authentication and several parameters.
With AFNetworking 1.x I had an AFHTTPClient that I could use to get a NSURLRequest from and then pass that into the UIImageView setImageWithURLRequest:... method. I can't find anything in the AFHTTPRequestOperationManager that returns an NSURLRequest.
Am I just blind?
Have you looked into AFURLSessionManager? Looks like it supports downloading and you set the NSURLRequest yourself so you could set the params and all that:
https://github.com/AFNetworking/AFNetworking#afurlsessionmanager
It looks like there is no way to have the AFHTTPRequestOperationManager make a NSURLRequest for me. However, the requestSerializer will make one so I am using that. The only problem is that the requestSerializer doesn't know about the baseURL, so I have to pull that out of the operation manager first.
NSString* fullPath = [self.HTTPRequestOperationManager.baseURL.absoluteString stringByAppendingString: path];
NSMutableURLRequest* urlRequest = [self.HTTPRequestOperationManager.requestSerializer requestWithMethod: #"GET" URLString: fullPath parameters: parameters error: &error];
I'll have to suggest this as a feature request. I should not have to pull one piece of information out the a RequestOperationManager only to send it to another piece of the same RequestOperationManager.

ASIHTTPRequest addRequestHeader issue

I am using ASIHttpRequest and make use GET method to send header to server. I call method addRequestHeader like this
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:#"myHeader" value:#"abc"];
It's not working. But if I use NSMutableURLRequest to add header and request to server, it works.
I don't know anything wrong when calling addRequestHeader methods for ASIHTTPRequest library.
Have anyone seen this issue?
Wow ok so, yeah if this is a new app, please do NOT use ASIHttpRequest. It has long been supplanted by the delightful AFNetworking.
If this is an existing application, you really should work on a migration plan off ASI.
However, in an attempt to actually answer your question - that is the appropriate setup per the documentation, and is how I used to use it. My guess is something is broken under the covers and judging from a basic google request, there are issues with iOS 7 including memory leaks and requests just failing.
You can do it via NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.abc.com"]];
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:#"AAA" forHTTPHeaderField:#"Hello-there"];
request = [mutableRequest copy];
NSLog(#"%#", request.allHTTPHeaderFields);
Hope this helps .. :)

AFNetworking's requestWithMethod:path:parameters: Returning NSMutableURLRequest That Doesn't Persist

I am using AFNetworking version 0.10.x for iOS 4.3 compatibility. This version of AFNetworking does not use arc so AFHTTPClient as well as the other AFNetworking classes have the fno-objc-arc flag applied to each of them (the rest of my project is using ARC). In a function I have the following code:
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:url]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *urlRequest = [httpClient requestWithMethod:method
path:nil
parameters:adjustedParameters];
NSLog(#"returning: %#", urlRequest);
return urlRequest;
The problem I am seeing is that urlRequest is always returned as NULL.
I have even stepped through this code with a debugger:
As you can see, the log is printing out a value of null while the debugger is stating that url request points to a valid memory address. I am running my project on iOS 6.0 with Xcode 4.6.3. Any idea as to what the issue is?
The problem was that the method requestWithMethod:path:parameters: was returning a nil request object because I was passing nil as the path parameter. My code should have read:
urlRequest = [httpClient requestWithMethod:method
path:#""
parameters:adjustedParameters];
try setting the default header's accept return type.
for example in my code I have:
[client setDefaultHeader:#"Accept" value:#"text/html"] it could also be application/json depending on your return type header.
see here for more details (although the response the OP is getting in the mentioned link is different than yours.. it may be due the difference between ARC and non-ARC)

Read status line (status code and reason phrase) using AFNetworking's AFHTTPRequestOperation

I am switching from ASIHTTPRequest to AFNetworking in an iOS application.
RF2616 (HTTP/1.1) defines a "status-line" by the combination of "Status Code" and "Reason Phrase". Sometimes the server adds some specific information in this "Reason Phrase" and I found it rather handy that ASIHTTPRequest allowed me to access it easily via
ASIHTTPRequest *request = ...;
NSString *reason = request.responseStatusMessage;
My problem is that I can't find any way to do it with AFHTTPRequestOperation
NSMutableURLRequest *request = ...;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
In both completion/failure block of the operation i can get the status code by doing:
int statusCode = [operation.response statusCode];
But I can't find where to get the "Reason Phrase".
Some answers on StackOverflow suggest that it would be located in one of the entries of [operation.response allHeaderFields] but it's not.
An answer to this question << Can I access "Reason Phrase" from the HTTP Status-Line in NSHTTPURLResponse >>
suggests changing how the server behaves but that's not always an available option.
Any idea?
NSHTTPURLResponse does not implement a method that exposes the data, and the HTTP RFC does not a require it to. AFNetworking uses NSURLRequest / NSURLResponse, and so AFNetworking cannot give it to you either. ASIHTTPRequest can, on the other hand, because it uses the lower-level CFNetwork framework.
The advice given in the other answers, to move this information to your headers or body, is the best answer.
You can also file an enhancement request with Apple, and / or stick with ASIHTTPRequest.

Any alternative to NSURL?

A client is pondering development of an iPhone and iPad app and has asked an odd question: Is there any way to send data from an iOS device to a server other than using NSURL?
I think you could try using a POST request
responseData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:#"http://www.domain.com/your/servlet"]];
NSString *params = [[NSString alloc] initWithFormat:#"foo=bar&key=value"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
That way the data would go inside the body of the request and not on the URL itself
The NSURL API isn't a way of getting data from a server. It's just a class for storing a URL. I suppose if you really want to avoid NSURL as much as possible, you could store URLs in an NSString, and then use appropriate APIs to convert it into an NSURL right before you use it.
To get data from a server, you would use either the NSURLSession API (modern) or NSURLConnection API (kind of crufty). Either is a fairly straightforward way to fetch data from an HTTP or HTTPS URL.
If you don't want to use either of those URL fetching APIs for some reason, you can write your own code using sockets or grab libcurl (MIT license) and link it into your app. Be aware that if you write your own socket code or use libcurl, assuming you're writing code for iOS, you'll need to occasionally use high-level APIs such as NSURL or CFHost to wake up the cellular radio.

Resources