Giter VIP home page Giter VIP logo

restframework's Introduction

RESTframework is no longer actively maintained and has been deprecated in favor of AFNetworking which is de facto standard these days.

Introduction

RESTframework is a lightweight Cocoa framework for working with RESTful web services. It's designed for simplicity rather than robustness. Although it will be enhanced in time with more and more features, simplicity of usage will be kept priority. RESTframework supports GET, POST, PUT and DELETE HTTP verbs wrapped in a simple interface keeping the implementation abstract.

How to use

To start, simply copy all files from RFClasses directory into your Xcode project. Build & make sure you get no errors. Now simply import RFRequest.h, RFResponse.h and RFService.h and you're ready to roll.

Xcode project also comes with a simple Flickr demo for echoing input and searching.

ARC

RESTframework classes are (still) non-ARC classes for compatibility with older Xcode versions. This will probably change in the near future. In order to use RESTframework in your ARC project use -fno-objc-arc flag in "Build Phases" > "Compile Sources" section (http://f.cl.ly/items/2V0y3k2D0r2N2P2l1h44/no-ARC.png)

Examples

GET example

To GET a resource from URL that looks like http://myapi.example/api/v1/resources/myresource?param1=2&param2=test, we would do the following.

RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:@"http://myapi.example/"] type:RFRequestMethodGet resourcePathComponents:@"api", @"v1", @"resources", @"myresource", nil];

[r addParam:@"2" forKey:@"param1"];
[r addParam:@"test" forKey:@"param2"];

//now execute this request and fetch the response in a block
[RFService execRequest:r completion:^(RFResponse *response) {
  NSLog(@"%@", response); //print out full response
  NSLog(@"%@", response.dataValue); //dataValue is received response as NSData (e.g. you can do [response.dataValue objectFromJSONData])
}];

POST example

To POST application/x-www-form-urlencoded data to a resource at URL that looks like http://myapi.example/api/v1/resources, we would do the following.

RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:@"http://myapi.example/"] type:RFRequestMethodPost resourcePathComponents:@"api", @"v1", @"resources", nil];

[r addParam:@"2" forKey:@"param1"];
[r addParam:@"test" forKey:@"param2"];

//now execute this request and fetch the response in a block
[RFService execRequest:r completion:^(RFResponse *response){
	NSLog(@"%@", response); //print out full response
	NSLog(@"%@", response.dataValue); //dataValue is received response as NSData (e.g. you can do [response.dataValue objectFromJSONData])
}];

Now, RESTframework currently has body encoding helpers for x-www-form-urlencoded and multipart/form-data (you can post files etc...). Third option is raw bytes where user is responsible for assigning NSData bytes for HTTP request body and setting appropriate content type.

POST files (multipart/form-data)

RFRequest* r = [RFRequest requestWithURL:[NSURL URLWithString:@"test/"] type:RFRequestMethodPost bodyContentType:RFRequestBodyTypeMultiPartFormData resourcePathComponents:@"sub1", @"sub2", nil];

//add files...
[r addData:[NSData dataWithContentsOfFile:@"myfilepath"] withContentType:@"image/png" forKey:@"mypic.png"]

POST request with custom body encoding (content type)

RFRequest* r = [RFRequest requestWithURL:[NSURL URLWithString:@"test/"] type:RFRequestMethodPost bodyContentType:RFRequestBodyTypeRawBytes resourcePathComponents:@"sub1", @"sub2", nil];	
	
//assign custom body data
NSString *xmlDataString = @"<Node>data</Node>";
r.bodyData = [xmlDataString dataUsingEncoding:NSUTF8StringEncoding];
  
//set custom content type
r.rawBytesBodyContentType = @"application/xml";

License

RESTframework is licensed under GNU LGPL v2.1

Credits

restframework's People

Contributors

ivasic avatar pesicvladica 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

Watchers

 avatar  avatar  avatar

restframework's Issues

Can't upload a file

Hi,
I'm trying to upload a file from my iPhone ios 6.
In wireshark, I see that the data is missing, I only see Content-Disposition and Content-Type but no data.
Content-Disposition: form-data; name="file"; filename="mypic.jpeg"\r\n
Content-Type: image/jpeg\r\n\r\n
and after that - NOTHING

Here is my code:
RFRequest* r = [RFRequest requestWithURL:[NSURL URLWithString:url] type:RFRequestMethodPost bodyContentType:RFRequestBodyTypeMultiPartFormData resourcePathComponents:nil];

//add files...
NSData *data = UIImageJPEGRepresentation(self.selectedImage, 1.0);
[r addData:data withContentType:@"image/jpeg" forKey:@"mypic.jpeg"];

ios 4 support

Did this framework support ios4?
I have a concern, since you are using objC block e.g. completion:^(RFResponse *response)

Exception handling

Can you post some examples how to handle exceptions?

let say you do
[RFService execRequest:r completion:^(RFResponse *response) {

but server is not available or the document is not found (404)

Typo in github documentation

I think that instead of variable r should be response

//now execute this request and fetch the response in a block
[RFService execRequest:r completion:^(RFResponse *response) {
  NSLog(@"%@", r); //print out full response
  NSLog(@"%@", r.dataValue); //dataValue is received response as NSData (e.g. you can do [r.dataValue objectFromJSONData])
}];

and in the second code block example, it should be RFRequestMethodPostinstead of RFRequestMethodGet

Post json

Why did you comment out line 348 in file RFRequest.m

        /*case RESTRequestBodyTypeJSON:
         return @"application/json";*/

How do I post json format?

Post file and extra parameters

Hi Ivan!

Thank you for the framework.

How can I add upload a file and additional parameters to the same request?

I've got the following code:

RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:BASE_URL] type:RFRequestMethodPost bodyContentType:RFRequestBodyTypeMultiPartFormData resourcePathComponents:token, @"resource", nil];

NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/tempImage.jpg"];    
[r addData:[NSData dataWithContentsOfFile:jpgPath] withContentType:@"image/jpeg" forKey:@"image.jpg"];

[r addParam:@"Some String" forKey:@"resource[string_value]"];
[r addParam:@"2012-06-10" forKey:@"resource[another_string_value]"];

Only one parameter is sent to the Rails API.

What am I missing?

Thanks

/ Ola

RESTframework & ARC

Hello,

I see you're deallocing & releasing your objects manually.

Does it means I have to deactivate ARC on my project to use your framework ? Or could i simply modify your sources to make it work with ARC ?

Thanks ;)

Keep it going, your framework really helped me understand the way of dealing with REST services !

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.