Skip to main content

fetch

Macro fetch 

Source
macro_rules! fetch {
    ($url:expr, &$client:ident) => { ... };
    ($url:expr, $headers:expr, &$client:ident) => { ... };
    ($url:literal, &$client:ident, $res_body_ty:ident, $res_ty:ty) => { ... };
    ($url:expr, &$client:ident, $res_body_ty:ident, $res_ty:ty) => { ... };
    ($url:expr, $headers:expr, &$client:ident, $res_body_ty:ident, $res_ty:ty) => { ... };
}
Expand description

Make a GET request to the specified URL.

The fetch! macro is a more generic version of the get! macro. Its first argument is a string literal or a variable. Arrows are used to specify the body serialization type and the output type.

You can use the JsonBody, XmlBody, MsgPack type for JSON, XML and MessagePack serialization.

To help understand the macro arguments, here is an example:

fetch!(url, &mut client, body, ty)

§Arguments

  • url - The URL to make the GET request to.
  • headers - The headers to send with request.
  • client - The client variable to use request.
  • res_body_ty - The body serialization format of response.
  • res_ty - The type of response.

Please note url can be a string literal or a variable.

§Example

let client = Client::new();
let response = fetch!("https://jsonplaceholder.typicode.com/posts", &client, JsonBody, Post);
assert_eq!(response.id, 1);