Code Monkey home page Code Monkey logo

svhttprequest's Introduction

SVHTTPRequest

SVHTTPRequest lets you easily interact with RESTful (GET, POST, DELETE, PUT) web APIs. It is blocked-based, uses NSURLConnection, ARC, as well as NSJSONSerialization to automatically parse JSON responses.

SVHTTPRequest features:

  • class methods for quickly making GET, POST, PUT, DELETE, HEAD and download requests.
  • completion block handler returning response (NSObject if JSON, otherwise NSData), NSHTTPURLResponse and NSError objects.
  • persistent basePath and basic authentication signing when using SVHTTPClient.
  • support for multipart/form-data parameters in POST and PUT requests.
  • talks with the network activity indicator (iOS only).

Installation

From CocoaPods

Add pod 'SVHTTPRequest' to your Podfile or pod 'SVHTTPRequest', :head if you're feeling adventurous.

Manually

If your project doesn't use ARC: you must add the -fobjc-arc compiler flag to SVHTTPRequest.m and SVHTTPClient.m in Target Settings > Build Phases > Compile Sources.

  • Drag the SVHTTPRequest/SVHTTPRequest folder into your project.
  • #import "SVHTTPRequest.h" (this will import SVHTTPClient as well)

Usage

(see sample Xcode project in /Demo)

The easiest way to make a request is using the SVHTTPRequest convenience methods:

[SVHTTPRequest GET:@"https://api.github.com/repos/samvermette/SVHTTPRequest"
        parameters:nil
        completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
            watchersLabel.text = [NSString stringWithFormat:@"SVHTTPRequest has %@ watchers", [response valueForKey:@"watchers"]];
        }];

If most of your requests are made to the same API endpoint, you should instead use SVHTTPClient so you can set parameters (basePath, cachePolicy, sendParametersAsJSON, "userAgent) that will be used for each request:

[[SVHTTPClient sharedClient] setBasePath:@"http://api.twitter.com/1/"];

[[SVHTTPClient sharedClient] GET:@"users/show.json"
                      parameters:[NSDictionary dictionaryWithObject:@"samvermette" forKey:@"screen_name"]
                      completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                          followersLabel.text = [NSString stringWithFormat:@"@samvermette has %@ followers", [response valueForKey:@"followers_count"]];
                      }];

You can have mutiple SVHTTPClient instances using the sharedClientWithIdentifier: method.

If you would like to set those properties on individual requests, you'll need to alloc/init the request, set the attributes, and then call start:

SVHTTPRequest *request = [[SVHTTPRequest alloc] initWithAddress:@"http://github.com/api/v2/json/repos/show/samvermette/SVHTTPRequest"
                                                         method:SVHTTPRequestMethodGET 
                                                     parameters:nil 
                                                     completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                                                         watchersLabel.text = [NSString stringWithFormat:@"SVHTTPRequest has %@ watchers", [[response valueForKey:@"repository"] valueForKey:@"watchers"]];
                                                     }];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
[request start];

Making a download request

You can tell SVHTTPRequest to save a GET response directly to disk and track the progress along the way:

[SVHTTPRequest GET:@"http://example.com/db.sqlite.zip" 
        parameters:nil 
        saveToPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"store.zip"]
          progress:^(float progress) {
              progressLabel.text = [NSString stringWithFormat:@"Downloading (%.0f%%)", progress*100];
          } 
        completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
            progressLabel.text = @"Download complete";
            // process file
        }];

Cancelling requests

Make sure you cancel requests for which the user isn't waiting on anymore:

SVHTTPRequest *request = [SVHTTPRequest GET:@"http://api.twitter.com/1/users/show.json"
                                 parameters:[NSDictionary dictionaryWithObject:@"samvermette" forKey:@"screen_name"]
                                 completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                                     NSLog(@"%@", response);
                                 }];
   
[request cancel];

If you're using SVHTTPClient, you can do that by calling cancelRequestsWithPath: or cancelAllRequests.

Disabling logging

By default, SVHTTPRequest will log messages to the console every time a request is made. You can disable this by adding the compiler flag -DSVHTTPREQUEST_DISABLE_LOGGING to SVHTTPRequest.m in Target Settings > Build Phases.

Under the hood

All SVHTTPRequest requests are made asynchronously using NSURLConnection's built-in asynchronous methods. The completion block, however, is executed on the main thread. You should dispatch it to a separate thread/queue if it's resource intensive enough that it hogs the main thread. This can be done easily using Grand Central Dispatch:

completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        // cpu-intensive code
    });
}];

Credits

SVHTTPRequest is brought to you by Sam Vermette and contributors to the project. If you have feature suggestions or bug reports, feel free to help out by sending pull requests or by creating new issues. If you're using SVHTTPRequest in your project, attribution would be nice.

svhttprequest's People

Contributors

gcamp avatar harukizaemon avatar jogi avatar juliensaad avatar muteq avatar ocollet avatar pwightman avatar samvermette avatar tciuro avatar tonyarnold avatar uzysjung avatar wesbillman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

svhttprequest's Issues

`self.basePath` should be initialised to `@""`, not to `nil`

Currently, the URL is evaluated for SVHTTPClient using the following line:

NSString *completeURLString = [NSString stringWithFormat:@"%@%@", self.basePath, path];

If the basePath has not been set, this results in:

@"(null)http://someurl.com/"

I'd suggest that if this is how things are to remain that self.basePath should be initialised to @"", and not to nil

Add a way to customize/override logging

I think it would be beneficial to have SVHPPTRequest support custom logging. Personally I'd like to hook it into CocoaLumberjack but I'm sure there are other custom loggers that are reasonably popular. Only having an option to disable logging with a compile-time switch is cumbersome (especially if SVHTTPRequest is installed via CocoaPods).

I am not sure what the best approach is - providing an optional logger block callback? SVHTTPRequestLogger delegate?

SVHTTPRequest connection:didFailWithError called twice on timeout

This code sometimes results in the completion block being called twice:

   SVHTTPRequest *request = [[SVHTTPRequest alloc] initWithAddress:kJTWC
                                                             method:SVHTTPRequestMethodGET
                                                         parameters:nil
                                                         completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {


                                                             if(error != nil || [urlResponse statusCode] == 0){

                                                                 NSLog(@"Error is  %@", [error localizedDescription]);

                                                                 if ([urlResponse statusCode] == 0) {
                                                                     NSLog(@"the request failed entirely");
                                                                 }

                                                             }

                                                             [request release];


                                                        }];
    request.timeoutInterval = 10.0;
    [request start];

I think it's called once via the timer and once from the actual NSURLConnection. See below where I have set a custom error "Error set by requestTimeout()"

[26571:c07]  SVHTTPRequestresponse start
[26571:c07] -[SVHTTPRequest start] line 291 $ timeout timeoutInterval = 10
[26571:c07] [GET] http://www.usno.navy.mil/JTWC
[26571:c07] -[SVHTTPRequest requestTimeout] line 383 $ requestTimeout
[26571:c07] -[SVHTTPRequest connection:didFailWithError:] line 445 $ connection didFailWithError
[26571:c07] -[SVHTTPRequest connection:didFailWithError:] line 445 $ connection didFailWithError
[26571:c07] __55-[SVHTTPRequest callCompletionBlockWithResponse:error:]_block_invoke_0 line 468 $ self.operationCompletionBlock
[26571:c07] Error is  The operation timed out. Error set by requestTimeout()
[26571:c07] -[SVHTTPRequest finish] line 322 $ FINISH
[26571:c07] __55-[SVHTTPRequest callCompletionBlockWithResponse:error:]_block_invoke_0 line 468 $ self.operationCompletionBlock
[26571:c07] Error is  The request timed out.
[26571:c07] -[SVHTTPRequest finish] line 322 $ FINISH

So finish() isn't called in time I think?

Any ideas how to avoid this?
thanks

Mac OSX compatibility?

I noticed in the CocoaPods podspec for this library that only iOS is supported, though in this repo it says Mac OSX Lion+ is supported. Is one of them not current? I may attempt to port an iOS app to Mac and I'd like to bring over all the networking code if possible.

SVHTTPRequest should not judge http status code != 200 as NSError, it should handled manually by programmer

Reason:
Some API does set HTTP status code to 400, whenever we did not comply with the parameters.

Let's say when we want to login, and the email we put is not a valid format, and the server API end point will return HTTP status code 400 with JSON body containing the error message.

At current code, this will trigger NSError.

A good approach is to let the programmer handle the status code by him/her self.

[[SVHTTPClient sharedClient] POST:@"users/login"
                           parameters:parameters
                           completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                                if (error != nil) {
                                    // This error here should be only ios error                                 
                                    // Handle error

                                    return;
                                }                           

                                // HTTP status code should be handled manually by the programmer here                              

                                if (urlResponse.statusCode == 200) {
                                    // Proceed
                                } else {
                                    // Show dialog box to user
                                }
                           }];

To fix this we can remove checking serverError in SVHTTPRequest.m on this function

- (void)callCompletionBlockWithResponse:(id)response error:(NSError *)error 

Thank you

Please add semantic version tags.

I’ve recently added SVHTTPRequest to the CocoaPods package manager repo.

CocoaPods is a tool for managing dependencies for OSX and iOS Xcode projects and provides a central repository for iOS/OSX libraries. This makes adding libraries to a project and updating them extremely easy and it will help users to resolve dependencies of the libraries they use.

However, SVHTTPRequest doesn't have any version tags. I’ve added the current HEAD as version 0.0.1, but a version tag will make dependency resolution much easier.

Semantic version tags (instead of plain commit hashes/revisions) allow for resolution of cross-dependencies.

In case you didn’t know this yet; you can tag the current HEAD as, for instance, version 1.0.0, like so:

$ git tag -a 1.0.0 -m "Tag release 1.0.0"
$ git push --tags

BaseHeader

These days, REST API server sometimes using HTTP headers to receive input such as Accept-Language and other headers.

So i think it would be nice if we also have 'BaseHeaders'. So we can have preset headers just like BaseParameters.

Thank you

Unable to send a dictionary of dictionaries via GET.

I've seen that when you use a GET request, the parameter cannot be a dictionary of dictionaries. Why is that? The code is this one:

  • (NSString_)parameterStringForDictionary:(NSDictionary_)parameters {
    NSMutableArray *stringParameters = [NSMutableArray arrayWithCapacity:parameters.count];

    [parameters enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    if([obj isKindOfClass:[NSString class]]) {
    [stringParameters addObject:[NSString stringWithFormat:@"%@=%@", key, [obj encodedURLParameterString]]];
    }
    else if([obj isKindOfClass:[NSNumber class]]) {
    [stringParameters addObject:[NSString stringWithFormat:@"%@=%@", key, obj]];
    }
    else
    [NSException raise:NSInvalidArgumentException format:@"%@ requests only accept NSString, NSNumber and NSData parameters.", self.operationRequest.HTTPMethod];
    }];

    return [stringParameters componentsJoinedByString:@"&"];
    }

Logical issue when sending parameters as JSON

There is a logical issue when creating request with sendParametersAsJSON=YES;

In SVHTTPClient.m:

  • (SVHTTPRequest_)queueRequest:(NSString_)path
    method:(SVHTTPRequestMethod)method
    parameters:(NSDictionary_)parameters
    saveToPath:(NSString_)savePath
    progress:(void (^)(float))progressBlock
    completion:(SVHTTPRequestCompletionHandler)completionBlock {

{...}
SVHTTPRequest *requestOperation = [(id)[SVHTTPRequest alloc] initWithAddress:completeURLString
method:method
parameters:mergedParameters
saveToPath:savePath
sendAsJSON:self.sendParametersAsJSON
progress:progressBlock
completion:completionBlock];
return [self queueRequest:requestOperation];
}

  • (SVHTTPRequest_)queueRequest:(SVHTTPRequest_)requestOperation {
    requestOperation.sendParametersAsJSON = self.sendParametersAsJSON;
    {...}
    return requestOperation;
    }

requestOperation.sendParametersAsJSON will be set to YES after initialization, but in SVHTTPRequest.m addParametersToRequest: method is called inside of init method and at that time sendParametersAsJSON is always NO. As a result, I'm unable to send parameters as JSON :)

Uploading image most of the time will timeout

Hello, i tried to send a POST with image data, most of the time it will timeout

Tried to change timeout interval but no luck,

[SVHTTPClient sharedClient].timeoutInterval = 6000;

Even i changed to 10 minutes, it will just time out in few seconds.

Any ideas?

setNetworkActivityIndicatorVisible

Is setNetworkActivityIndicatorVisible only called if you are using the [SVHTTPClient sharedClient]?

It seems that if you use SVHTTPRequest GET or SVHTTPRequest *request = [[SVHTTPRequest alloc] initWithAddress that the client property isn't initialised?

Thanks
James

setting queue size

As far as I see, the requests are added to a queue - it would be great if there's the possibility to set the queue's size, i.e. how many requests can be done simultaneously.

Bump podspec version?

Hi Sam,
Would you mind to bump podspec version as it's quite out dated now.
Thanks :)

Not using didReceiveAuthenticationChallenge:?

Hello Sam,

I was just wondering... is there a reason why you're not implementing the challenge callback?:

- (void)connection:(NSURLConnection *)aConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

That would take care of BASIC and DIGEST-based authentication.

Disabling Logging

Hey Sam,

I've updated to the latest version and followed the instructions exactly as they are written here ...

You can disable this by adding the compiler flag -DSVHTTPREQUEST_DISABLE_LOGGING to SVHTTPRequest.m in Target Settings > Build Phases

But I'm still seeing output in the log. Any ideas?

-Mic

How to make a request wait until it's completed?

Hello,

How to make a request wait until it's completed? (Like sync request instead of async)

My current condition is looks like these

  1. Call API token request if the token is expired, otherwise just skip to number 2.
  2. Call some API request with the token.

In these requirement, when the token is expired i have to wait until it's finished, before i call the next API.

Here is pseudo code example for case above

// Check if token already expired
if ([self tokenExpired]) {
    // Refresh token by calling refresh token API (This need to wait until completed.. or sync)
    // Inside refresh token method, there is [SVHTTPClient sharedClient] POST ...  to request the token
    [self refreshToken];
}

// Call some API request with the token
[SVHTTPClient sharedClient] GET.... 

Tried GCD but no luck... probably i did it wrong.
Please shed some light

Thank you

Upload PDF in multipart form data

Hello,
I want to send to a service a pdf file in multipart form,
but the server does not receive anything? I can send images using UIImageJPEGRepresentation,
but I can not send PDF files

Nested Parameters?

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.

Error:(55, 1) property with 'retain (or strong)' attribute must be of object type

I do not understand what the problem is.
iOS8.1 (iPad 2)

#import "RestClient.h"
#import "SVHTTPRequest.h"

@implementation RestClient

-(id)init {
    [[SVHTTPClient sharedClient] setBasePath:@"http://172.16.7.14/"];
    [[SVHTTPClient sharedClient] GET:@"questions/all"
                          parameters:nil
                          completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                              NSLog(@"\n%@\n%@",response,urlResponse);
                          }];
    return self;
}

@end

Add cachePolicy as an option while making request?

The default cache policy with a NSURLRequest is NSURLRequestUseProtocolCachePolicy. This cachePolicy will cause issues in situations where the data is changing rapidly. Sometimes it is best to always ignore local cache data while making a request(NSURLRequestReloadIgnoringLocalCacheData does the job). Is it possible to add an option to specify a custom cache policy for the http request?

'PUT' doesn't work in rails . because of didn't define Content-Type.

I solved this problem.

please check.

SVHTTPRequest.m
line 238

  if(!hasData) {
            const char *stringData = [[self parameterStringForDictionary:paramsDict] UTF8String];
            NSMutableData *postData = [NSMutableData dataWithBytes:stringData length:strlen(stringData)];

            [self.operationRequest setValue:@"application/x-www-form-urlencoded"             
             forHTTPHeaderField:@"Content-Type"];

            [self.operationRequest setHTTPBody:postData];
        }

Why is [SVHTTPRequest start] being executed on the main thread?

Hi Sam, this is not an issue but a question.

In [SVHTTPRequest start] you have written:

- (void)start {

    if(self.isCancelled) {
        [self finish];
        return;
    }

    if(![NSThread isMainThread]) { // NSOperationQueue calls start from a bg thread (through GCD), but NSURLConnection already does that by itself
        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
        return;
    }

    ...
}

Question: isn't executing the SVHTTPRequest on the main thread defeating the purpose of NSOperationQueue's concurrency model? I can see that one would have to do that in order to call the completion/failure block on the main thread. But then, unless I'm mistaken, it seems to me that launching several SVHTTPRequest would all end up on the main thread, right? Wouldn't these operations saturate the same core?

Thank you,

-- Tito

Unknown type name 'UIBackgroundTaskIdentifier'

Don't know if this lib is supporting iOS8, but I get this message after installing it and trying to run a simple GET request.

 SVHTTPRequest/SVHTTPRequest.m:46:34: Unknown type name 'UIBackgroundTaskIdentifier'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.