Asana API error - unsupported browser when using an ios client - objective-c

I am sending an API call to Asana from an iOS client and am getting an HTML page with the title: Asana - Unsupported Browser which tells me something is wrong with the way I make the call.
My API calling code is as follows:
Client Settings are here:
- (id) initHTTPClient {
self = [super initWithBaseURL:[NSURL URLWithString:ASANA_BASE_URL]];
if (self) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:#"Accept" value:#"application/json"];
[self setParameterEncoding:AFJSONParameterEncoding];
[self setAuthorizationHeaderWithUsername:ASANA_API_KEY password:[NSString string]];
}
return self;
}
API call is constructed here:
- (void) getCurrentUser {
NSDictionary *parameters = [NSDictionary dictionary];
NSMutableURLRequest *request = [self requestWithMethod:#"GET" path:#"/users/me.json" parameters:parameters];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error;
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
NSLog(#"Success, here's what we got: %#",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure, error: %#",error.debugDescription);
}];
[operation start];
}

Related

AFNetworking JSON Request, neither success nor error block called

I'm trying to learn AFNetworking so have written a simple block. I'm trying to retrieve & log the json from the site url below.
NSString *string =
#"http://transportapi.com/v3/uk/bus/stop/490012745J/live.json?api_key=6ee115459cbeccdb902b14d39b61330d&app_id=9deefeb1&group=route";
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *mydict = (NSDictionary *)responseObject;
NSString *key;
for(key in mydict){
NSLog(#" key %#", key);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"ERROR");
}];
But neither the success or the failure block is being called. Can someone point out what I've done wrong?
You're not actually firing the operation you need to add
[[NSOperationQueue mainQueue] addOperation:operation];

Can't get HTML code AFNetworking 2.0

I tried to make GET HTTP response. I need to get the html code for the subsequent parsing, but responseObject is nil.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager GET:#"http://www.example.com/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error;
HTMLParser *parser = [[HTMLParser alloc] initWithString:responseObject error:&error];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
For get html code we will need to build a custom response serializer to decode the NSData response from the web server into a NSString. We will need to subclass AFHTTPResponseSerializer and implement the following method:
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error
{
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
Why for example you have not use this solution below instead of subcalssing. It does the same thing, but you don't need to create additional files, just for overload one method.
So you can just add encoding your responseObjet in the block for example, and it will work as well. I am using POST in my example but it should work with GET in the same way but without parameters, but idea of the just conversation.
+ (void)makeRequestWithParams:(NSDictionary *)params
success:(OperationCompletionBlock)success
failure:(OperationCompletionBlock)failure
{
NSString *path = #"http://www.example.com/";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFCompoundResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString* encodedString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"%#", encodedString);
success(nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
failure(nil);
}];
}

i want to return data inside a block by using AFNetworking

I have a function using AFJSONRequestOperation, and I wish to return the result only after success. Could you point me in the right direction? I'm still a bit clueless with blocks and AFNetworking specifically.
It would have been better if you had posted your code
Using __block you can use variable inside block
__block NSString *msg;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[hud hide:YES];
NSLog(#"Success %#", operation.responseString);
NSDictionary *message = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
NSLog(#"%#",message);
msg = message
ALERT(#"Posted", message[#"message"]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"error: %#", operation.responseString);
NSLog(#"%#",error);
}];
[operation start];

AFHTTPClient subclass with custom NSMutableURLRequest objects

My app is using AFNetworking for twitter API access and I've created a twitter api client by subclassing AFHTTPClient:
#import "AFHTTPClient.h"
#interface TwitterAPIClient : AFHTTPClient
+ (TwitterAPIClient *)sharedClient;
#end
#import "TwitterAPIClient.h"
#import "AFJSONRequestOperation.h"
static NSString * const kAFTwitterAPIBaseURLString = #"http://api.twitter.com/1/";
#implementation TwitterAPIClient
+ (TwitterAPIClient *)sharedClient {
static TwitterAPIClient *_sharedClient = nil;
static dispatch_once_t TwitterAPIClientToken;
dispatch_once(&TwitterAPIClientToken, ^{
_sharedClient = [[TwitterAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFTwitterAPIBaseURLString]];
});
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:#"Accept" value:#"application/json"];
return self;
}
#end
If I use getPath's & postPath's on the TwitterAPIClient, the API client returns JSON responses correctly, as I register a AFJSONRequestOperation as the operation class.
However, Sometimes, I need to create custom NSMutableURLRequest requests and not use the getPath's & postPath's AFHTTPClient functions.
When I use these requests, the response is getting back from the client is standard NSData and not NSDictionary as I get from AFJSONRequestOperation.
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1.1/account/verify_credentials.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[self.auth authorizeRequest:request];
AFHTTPRequestOperation* apiRequest = [[TwitterAPIClient sharedClient] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, NSDictionary* responseObject) {
[self createAccount];
self.account.username = [responseObject objectForKey:#"screen_name"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate didProfileLoaded:self.account];
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (error!=nil) {
NSString* errorMessage = nil;
NSString* errorData = [error.userInfo objectForKey:NSLocalizedRecoverySuggestionErrorKey];
if (errorData!=nil) {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:[errorData dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
if (json!=nil && error==nil) {
NSArray* errorMeta = [json objectForKey:#"errors"];
if (errorMeta!=nil) {
errorMessage = [[errorMeta objectAtIndex:0] objectForKey:#"message"];
}
} else {
errorMessage = errorData;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate didUpdateFailed:errorMessage];
});
}
}];
[[TwitterAPIClient sharedClient] enqueueHTTPRequestOperation:apiRequest];
Is there a way I can force these AFHTTPRequestOperation to be created as AFJSONRequestOperation objects?
Just create an AFJSONRequestOperation directly, like so:
AFJSONRequestOperation* apiRequest = [[AFJSONRequestOperation alloc] initWithRequest:request];
[apiRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// ....
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// ...
}];
More examples here and here.

ASIFormDataRequest in AFNetworking?

I have some code in ASIHTTP, but I want to move on AFNetworking.
I used ASIFormDataRequest for some POST requests and this code works fine:
NSURL *url = [NSURL URLWithString:#"http://someapiurl"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"123" forKey:#"phone_number"];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSLog(#"Response: %#", [[request responseString] objectFromJSONString]);
}
but, when I tried to do the same with AFNetworking, I got in problem with content-type (I guess).
This is AFNetworking code, and it doesn't work:
NSURL *url = [NSURL URLWithString:#"http://dev.url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"123", #"phone_number",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:#"POST" path:#"/api/get_archive" parameters:params];
[request setValue:#"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:#"Content-Type"];
AFHTTPRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest
*request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Response: %#", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
NSLog(#"Error: %#", error);
}];
[operation start];
URL is fine, this is checked.
I'm getting from server this:
{NSErrorFailingURLKey=http://dev.thisapiurl, NSLocalizedDescription=Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html}
The problem you're having is because you are instantiating an AFJSONRequestOperation, which by default expects a JSON-friendly response type. Are you expecting a JSON response? If not, you should use a less-specific Request class. For example, you could use HTTPRequestOperationWithRequest: .
NSURL *url = [NSURL URLWithString:#"http://dev.url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"123", #"phone_number",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:#"POST" path:#"/api/get_archive" parameters:params];
[request setValue:#"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:#"Content-Type"];
//Notice the different method here!
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Response: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(#"Error: %#", error);
}];
//Enqueue it instead of just starting it.
[httpClient enqueueHTTPRequestOperation:operation];
If you have more specific request/response types (JSON, XML, etc), you can use those specific AFHTTPRequestOperation subclasses. Otherwise, just use the vanilla HTTP one.
I recently went through the same thing as you. Here is a custom class I wrote to handle pretty much any network request.
NetworkClient.h:
//
// NetworkClient.h
//
// Created by LJ Wilson on 3/8/12.
// Copyright (c) 2012 LJ Wilson. All rights reserved.
//
#import <Foundation/Foundation.h>
extern NSString * const ACHAPIKey;
#interface NetworkClient : NSObject
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
block:(void (^)(id obj))block;
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
syncRequest:(BOOL)syncRequest
block:(void (^)(id obj))block;
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
syncRequest:(BOOL)syncRequest
alertUserOnFailure:(BOOL)alertUserOnFailure
block:(void (^)(id obj))block;
+(void)handleNetworkErrorWithError:(NSError *)error;
+(void)handleNoAccessWithReason:(NSString *)reason;
#end
NetworkClient.m:
//
// NetworkClient.m
//
// Created by LJ Wilson on 3/8/12.
// Copyright (c) 2012 LJ Wilson. All rights reserved.
//
#import "NetworkClient.h"
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
#import "SBJson.h"
NSString * const APIKey = #"APIKeyIfYouSoDesire";
#implementation NetworkClient
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
block:(void (^)(id obj))block {
[self processURLRequestWithURL:url andParams:params syncRequest:NO alertUserOnFailure:NO block:^(id obj) {
block(obj);
}];
}
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
syncRequest:(BOOL)syncRequest
block:(void (^)(id obj))block {
if (syncRequest) {
[self processURLRequestWithURL:url andParams:params syncRequest:YES alertUserOnFailure:NO block:^(id obj) {
block(obj);
}];
} else {
[self processURLRequestWithURL:url andParams:params syncRequest:NO alertUserOnFailure:NO block:^(id obj) {
block(obj);
}];
}
}
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
syncRequest:(BOOL)syncRequest
alertUserOnFailure:(BOOL)alertUserOnFailure
block:(void (^)(id obj))block {
// Default url goes here, pass in a nil to use it
if (url == nil) {
url = #"MyDefaultURLGoesHere";
}
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:params];
[dict setValue:APIKey forKey:#"APIKey"];
NSDictionary *newParams = [[NSDictionary alloc] initWithDictionary:dict];
NSURL *requestURL;
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:requestURL];
NSMutableURLRequest *theRequest = [httpClient requestWithMethod:#"POST" path:url parameters:newParams];
__block NSString *responseString = [NSString stringWithString:#""];
AFHTTPRequestOperation *_operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
__weak AFHTTPRequestOperation *operation = _operation;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
responseString = [operation responseString];
id retObj = [responseString JSONValue];
// Check for invalid response (No Access)
if ([retObj isKindOfClass:[NSDictionary class]]) {
if ([[(NSDictionary *)retObj valueForKey:#"Message"] isEqualToString:#"No Access"]) {
block(nil);
[self handleNoAccessWithReason:[(NSDictionary *)retObj valueForKey:#"Reason"]];
}
} else if ([retObj isKindOfClass:[NSArray class]]) {
NSDictionary *dict = [(NSArray *)retObj objectAtIndex:0];
if ([[dict valueForKey:#"Message"] isEqualToString:#"No Access"]) {
block(nil);
[self handleNoAccessWithReason:[(NSDictionary *)retObj valueForKey:#"Reason"]];
}
}
block(retObj);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failed with error = %#", [NSString stringWithFormat:#"[Error]:%#",error]);
block(nil);
if (alertUserOnFailure) {
[self handleNetworkErrorWithError:operation.error];
}
}];
[operation start];
if (syncRequest) {
// Only fires if Syncronous was passed in as YES. Default is NO
[operation waitUntilFinished];
}
}
+(void)handleNetworkErrorWithError:(NSError *)error {
NSString *errorString = [NSString stringWithFormat:#"[Error]:%#",error];
// Standard UIAlert Syntax
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:#"Connection Error"
message:errorString
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[myAlert show];
}
+(void)handleNoAccessWithReason:(NSString *)reason {
// Standard UIAlert Syntax
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:#"No Access"
message:reason
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[myAlert show];
}
#end
This adds in a couple of features you may not need or want, feel free to modify it as you need as long as the Copyright section stays in place. I use that APIKey to validate the request came from my app and not someone trying to hack things.
Calling it (assuming you have included NetworkClient.h:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"ParamValue1", #"ParamName1",
#"ParamValue2", #"ParamName2",
nil];
[NetworkClient processURLRequestWithURL:nil andParams:params block:^(id obj) {
if ([obj isKindOfClass:[NSArray class]]) {
// Do whatever you want with the object. In this case, I knew I was expecting an Array, but it will return a Dictionary if that is what the web-service responds with.
}
}];
Also can:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"ParamValue1", #"ParamName1",
nil];
NSString *urlString = #"https://SuppliedURLOverridesDefault";
[NetworkClient processURLRequestWithURL:urlString
andParams:params
syncRequest:YES
alertUserOnFailure:NO
block:^(id obj) {
if ([obj isKindOfClass:[NSArray class]]) {
// Do stuff
}
}];
So it will take in any number of parameters, inject an APIKey or anything else if you want and return back either a Dictionary or an Array depending on the web-service. This does expect SBJson BTW.