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§
- Header
Iter - Iterator over request headers
- Header
Map - Map of HTTP message headers. Header keys are case-insensitive.
- Http
Message - Base message struct for Request and Response
- KeyValue
Iter - Iterator Over key/value parameters or cookies
- KeyValue
Map - Base struct for Request params and cookies. Keys are case-sensitive.
- Request
- HTTP request
- Response
- HTTP Response
Enums§
- Http
Method - 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