Crate roadrunner [] [src]

Roadrunner (RR)

Roadrunner is a rust rest client based on hyper project to provide an user friendly interface for use.

The API interface is partially inspired by unirest java library.

Example

#[macro_use] extern crate serde_derive;
extern crate tokio_core;

extern crate serde_json;
extern crate hyper;
extern crate roadrunner;

// need both RestClient and RestClientMethods
use roadrunner::RestClient;
use roadrunner::RestClientMethods;

use hyper::StatusCode;
use serde_json::Value;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Address {
  street: String,
  city: String,
}

fn main () {
    let mut core = tokio_core::reactor::Core::new().unwrap();
 
    let original_typed = Address {
        street: "135 College View Ave.".to_owned(),
        city: "San Francisco".to_owned(),
    };

    // Hit the local httpbin container in docker.
    // Please see a similar example in repo readme
    // (https://github.com/luanzhu/roadrunner) if you would like to try
    // this example.
    let response = RestClient::post("http://localhost:8000/post")
        .cookie("food", "bar")
        .authorization_bearer("QWxhZGRpbjpvcGVuIHNlc2FtZQ".to_string())
        .json_body_typed(&original_typed)
        .execute_on(&mut core)
        .unwrap();

    println!("{:?}", response);

    assert_eq!(*response.status(), StatusCode::Ok);

    let json_value = response.content().as_value().unwrap();
    assert_eq!(Value::String("application/json".to_owned()),
                json_value["headers"]["Content-Type"]);

    let data_str = json_value["data"].as_str().unwrap();

    println!("data_str : {:?}", data_str);

    let response_typed: Address = serde_json::from_str(data_str).unwrap();
    assert_eq!(original_typed, response_typed);
}

Usage

High level

High level API access is provided through RestClient and methods available in trait RestClientMethods.

Please refer to tests in the tests folder for more example.

Low level

For more control of request settings, use request_for_response. The high level RestClient is a thin layer on top of this function.

Supported Methods

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • OPTIONS

Structs

Content

Holds the body content of a response.

Response

Holds response received from server after a request is executed.

RestClient

RestClient is used to configure request.

Enums

Error

The error that can happen during a request.

Constants

CHROME_USER_AGENT

The user agent is used when user_agent_chrome is called.

DEFAULT_USER_AGENT

Default user agent will be sent with request if user agent is not specified.

FIREFOX_USER_AGENT

The user agent is used when user_agent_firefox is called.`

Traits

RestClientMethods

Provides a high level API that one can use to configure and execute a request.

Functions

request_for_response

This function provides a low level API interface that one can use if the high level API is not sufficient.