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
Iterator over request headers
Map of HTTP message headers. Header keys are case-insensitive.
Base message struct for Request and Response
Iterator Over key/value parameters or cookies
Base struct for Request params and cookies. Keys are case-sensitive.
HTTP request
HTTP Response
Enums
HTTP Request Method
Constants
Accept
header name
Content-Type
header value for JSON encoded in UTF-8
Content-Type
header name
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 203 NON-AUTHORIZATIVE INFORMATION status code
HTTP 204 NO CONTENT status code
HTTP 205 RESET CONTENT status code
HTTP 300 MULTIPLE CHOICES status code
HTTP 301 MOVED PERMANENTLY status code
HTTP 302 FOUND status code
HTTP 303 SEE OTHER status code
HTTP 305 RESET CONTENT status code
HTTP 307 TEMPORARY REDIRECT 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 409 CONFLICT status code
HTTP 410 GONE status code
HTTP 411 LENGTH REQUIRED status code
HTTP 413 PAYLOAD TOO LARGE status code
HTTP 414 URI TOO LARGE status code
HTTP 415 UNSUPORTED MEDIA TYPE status code
HTTP 417 EXPECTATION FAILED status code
HTTP 426 UPGRADE REQUIRED status code
HTTP 500 INTERNAL_SERVE_ERROR status code
HTTP 501 NOT IMPLEMENTED status code
HTTP 502 BAD_GATEWAY status code
HTTP 503 SERVICE UNAVAILABLE status code
HTTP 504 GATEWAY TIMEOUT status code
HTTP 505 HTTP VERSION NOT SUPPORTED status code