Crate wclient[][src]

Expand description

Simple Web Client for Rust

wclient is a lightweigh HTTP web client inspired on Python Requests.

It allows to send HTTP requests and receive the responses.

Note: This is a MVP implementation

Features

  • HTTP 1.1 Request and Response over plain TCP/IP
  • HTTP 1.1 Single Body

Future Features

  • HTTPS (v1.1)
  • Multipart
  • HTTP Session with Cookie Jar
  • HTTP Connection pooling

User Guide

To create a Request, it is needed a RequestBuilder.

The RequestBuilder has constructor functions for each HTTP method that requires the target url string: connect, delete, get, head, options, patch, post and put.

The ‘RequestBuilder’ allows to add name/value data for:

  • Headers through the functions header for a single header or headers for a set of headers. Header names are case-insensitive.
  • Parameters through the functions param for a single parameter or params for a set of parameters. Parameter names are case-sensitive.
  • Cookies through the functions cookie for a single cookie or cookies for a set of cookies. Cookie names are case-sensitive.

Also, the RequestBuilder allows to set HttpConfig configurations through the config function. Finally, to create a Request object, it has to be used the build member function of RequestBuilder.

Next example shows how to construct a GET request to the URL http://web.myservice.com/user?id=12345&name=John

use wclient::RequestBuilder;

let request = RequestBuilder::get("http://web.myservice.com/user")
    .header("Accept", "application/json")
    .param("id", "12345")
    .param("name", "John")
    .build(); 

The Request body can be set as a Vec<u8> using the function body or the function json with a JSON Object value form the crate json.

use wclient::RequestBuilder;
use json::object;
let data = object! {
   name: "John",
   surname: "Smith"
};
 
let mut request = RequestBuilder::post("Http://web.myservice.com/user")
    .header("Accept", "application/json")
    .param("id", "12345")
    .json(&data)
    .build();
// The Response
 
let response = request.send().unwrap();
// Check status code is 200 Success
assert_eq!(response.status_code(), 200);
let result_json = response.json();
// Check the request had JSON content
assert!(result_json.is_ok());
let result_data = result_json.unwrap();
// Print JSON object
println!("{:?}", result_data.as_str() )
 

After, created, the send function sends the request message to the target URL and returns a Result<Response, Error> value.

Modules

HTTP configuration settings

Structs

HTTP request

Helper builder for Request

HTTP Response An HTTP Response is formed by:

Enums

HTTP Request Method

Constants

HTTP 100 CONTINUE status code

HTTP 101 SWITCHING_PROTOCOLS status code

HTTP 200 OK status code

HTTP 201 CREATED status code

HTTP 202 ACCEPTED status code

HTTP 204 NO CONTENT status code

HTTP 205 RESET CONTENT status code

HTTP 400 BAD REQUEST status code

HTTP 401 UNAUTHORIZED status code

HTTP 402 BAD REQUEST status code

HTTP 404 NOT FOUND status code

HTTP 405 METHOD NOT ALLOWED status code

HTTP 406 NOT ACCEPTABLE status code

HTTP 408 REQUEST_TIMEOUT status code

HTTP 500 INTERNAL_SERVE_ERROR status code

HTTP 501 NOT IMPLEMENTED status code

Type Definitions

HTTP Response status code

List of Set-Cookie headers in a HTTP Response