Crate wrequest

Source
Expand description

§HTTP Request and Response implementation in Rust

wrequest is a crate that implements HTTP requests and responses.

§Request creation

use wrequest::Request;
use json::object;
 
// Create a PUT https://service.com/users/ request
 
let mut request = Request::put("https://service.com/users/");
 
// Add a ?client_id=1234 param
request.insert_param("client_id", "1234");
 
// Add request headers
request.insert_header("Content-Type", "application/json")
       .insert_header("Accept", "application/json");
 
// Add a request cookie
request.insert_cookie("session", "1234");
 
// Add a JSON Object as request body
let data = object! {
   name: "John",
   surname: "Smith"
};
 
// JSON Object is encoded at the body
request.set_json(&data);
 
assert_eq!(request.headers().get("Content-Type").unwrap(), "application/json" );
 

§Response creation

use wrequest::Response;
use wcookie::SetCookie;
use std::time::Duration;
use json::object;
 
 
// Create a HTTP 200 OK response
 
let mut response = Response::new(wrequest::HTTP_200_OK);
 
// Add response headers
response.insert_header("Content-Type", "application/json");
 
// Add a JSON Object as request body
let data = object! {
   name: "John",
   surname: "Smith"
};
 
// Add a `Set-Cookie` header
let mut cookie = SetCookie::new("session", "1234");
cookie.max_age = Some(Duration::new(3600, 0));
response.insert_cookie(cookie);
 
// JSON Object is encoded at the body
response.set_json(&data);
 
assert_eq!(response.headers().get("Content-Type").unwrap(), "application/json" );
 

§Future Features

  • Multipart

Structs§

HeaderIter
Iterator over request headers
HeaderMap
Map of HTTP message headers. Header keys are case-insensitive.
HttpMessage
Base message struct for Request and Response
KeyValueIter
Iterator Over key/value parameters or cookies
KeyValueMap
Base struct for Request params and cookies. Keys are case-sensitive.
Request
HTTP request
Response
HTTP Response

Enums§

HttpMethod
HTTP Request Method

Constants§

ACCEPT
Accept header name
APPLICATION_JSON
Content-Type header value for JSON encoded in UTF-8
CONTENT_TYPE
Content-Type header name
HTTP_100_CONTINUE
HTTP 100 CONTINUE status code
HTTP_101_SWITCHING_PROTOCOLS
HTTP 101 SWITCHING_PROTOCOLS status code
HTTP_200_OK
HTTP 200 OK status code
HTTP_201_CREATED
HTTP 201 CREATED status code
HTTP_202_ACCEPTED
HTTP 202 ACCEPTED status code
HTTP_203_NON_AUTHORIZATIVE_INFORMATION
HTTP 203 NON-AUTHORIZATIVE INFORMATION status code
HTTP_204_NO_CONTENT
HTTP 204 NO CONTENT status code
HTTP_205_RESET_CONTENT
HTTP 205 RESET CONTENT status code
HTTP_300_MULTIPLE_CHOICES
HTTP 300 MULTIPLE CHOICES status code
HTTP_301_MOVED_PERMANENTLY
HTTP 301 MOVED PERMANENTLY status code
HTTP_302_FOUND
HTTP 302 FOUND status code
HTTP_303_SEE_OTHER
HTTP 303 SEE OTHER status code
HTTP_305_RESET_CONTENT
HTTP 305 RESET CONTENT status code
HTTP_307_TEMPORARY_REDIRECT
HTTP 307 TEMPORARY REDIRECT status code
HTTP_400_BAD_REQUEST
HTTP 400 BAD REQUEST status code
HTTP_401_UNAUTHORIZED
HTTP 401 UNAUTHORIZED status code
HTTP_402_FORBIDDEN
HTTP 402 BAD REQUEST status code
HTTP_404_NOT_FOUND
HTTP 404 NOT FOUND status code
HTTP_405_METHOD_NOT_ALLOWED
HTTP 405 METHOD NOT ALLOWED status code
HTTP_406_NOT_ACCEPTABLE
HTTP 406 NOT ACCEPTABLE status code
HTTP_408_REQUEST_TIMEOUT
HTTP 408 REQUEST_TIMEOUT status code
HTTP_409_CONFLICT
HTTP 409 CONFLICT status code
HTTP_410_GONE
HTTP 410 GONE status code
HTTP_411_LENGTH_REQURED
HTTP 411 LENGTH REQUIRED status code
HTTP_413_PAYLOAD_TOO_LARGE
HTTP 413 PAYLOAD TOO LARGE status code
HTTP_414_URI_TOO_LONG
HTTP 414 URI TOO LARGE status code
HTTP_415_UNSUPORTED_MEDIA_TYPE
HTTP 415 UNSUPORTED MEDIA TYPE status code
HTTP_417_EXPECTATION_FAILED
HTTP 417 EXPECTATION FAILED status code
HTTP_426_UPGRADE_REQUIRED
HTTP 426 UPGRADE REQUIRED status code
HTTP_500_INTERNAL_SERVE_ERROR
HTTP 500 INTERNAL_SERVE_ERROR status code
HTTP_501_NOT_IMPLEMENTED
HTTP 501 NOT IMPLEMENTED status code
HTTP_502_BAD_GATEWAY
HTTP 502 BAD_GATEWAY status code
HTTP_503_SERVICE_UNAVAILABLE
HTTP 503 SERVICE UNAVAILABLE status code
HTTP_504_GATEWAY_TIMEOUT
HTTP 504 GATEWAY TIMEOUT status code
HTTP_505_HTTP_VERSION_NOT_SUPPORTED
HTTP 505 HTTP VERSION NOT SUPPORTED status code

Type Aliases§

HttpStatusCode