Struct RequestBuilder

Source
pub struct RequestBuilder { /* private fields */ }
Expand description

A builder for HTTP requests

Implementations§

Source§

impl RequestBuilder

Source

pub fn header(self, key: impl Into<String>, value: impl Into<String>) -> Self

Set a header

Examples found in repository?
examples/github_api.rs (line 35)
29async fn get_github_branch_advanced(repo: String) -> Result<GitHubBranch> {
30    let client = Client;
31    let url = format!("https://api.github.com/repos/{}/branches/master", repo);
32
33    let response = client
34        .get(url)
35        .header("Accept", "application/vnd.github.v3+json")
36        .header("User-Agent", "rust-wasm-fetch")
37        .send()
38        .await?
39        .error_for_status()?;
40
41    response.json().await
42}
43
44#[derive(Serialize)]
45struct CreateIssue {
46    title: String,
47    body: String,
48}
49
50/// POST request with JSON body
51async fn create_github_issue(repo: String, title: String, body: String) -> Result<Value> {
52    let client = Client;
53    let url = format!("https://api.github.com/repos/{}/issues", repo);
54
55    let issue = CreateIssue { title, body };
56
57    let response = client
58        .post(url)
59        .header("Accept", "application/vnd.github.v3+json")
60        .header("Authorization", "token YOUR_GITHUB_TOKEN")
61        .json(&issue)?
62        .send()
63        .await?
64        .error_for_status()?;
65
66    response.json_value().await
67}
Source

pub fn headers(self, headers: HashMap<String, String>) -> Self

Set multiple headers

Source

pub fn mode(self, mode: RequestMode) -> Self

Set the request mode (Cors, NoCors, SameOrigin)

Source

pub fn body(self, body: impl Into<String>) -> Self

Set the request body as a string

Source

pub fn json<T: Serialize>(self, json: &T) -> Result<Self>

Set the request body as JSON

Examples found in repository?
examples/github_api.rs (line 61)
51async fn create_github_issue(repo: String, title: String, body: String) -> Result<Value> {
52    let client = Client;
53    let url = format!("https://api.github.com/repos/{}/issues", repo);
54
55    let issue = CreateIssue { title, body };
56
57    let response = client
58        .post(url)
59        .header("Accept", "application/vnd.github.v3+json")
60        .header("Authorization", "token YOUR_GITHUB_TOKEN")
61        .json(&issue)?
62        .send()
63        .await?
64        .error_for_status()?;
65
66    response.json_value().await
67}
Source

pub async fn send(self) -> Result<Response>

Send the request and get a Response

Examples found in repository?
examples/github_api.rs (line 37)
29async fn get_github_branch_advanced(repo: String) -> Result<GitHubBranch> {
30    let client = Client;
31    let url = format!("https://api.github.com/repos/{}/branches/master", repo);
32
33    let response = client
34        .get(url)
35        .header("Accept", "application/vnd.github.v3+json")
36        .header("User-Agent", "rust-wasm-fetch")
37        .send()
38        .await?
39        .error_for_status()?;
40
41    response.json().await
42}
43
44#[derive(Serialize)]
45struct CreateIssue {
46    title: String,
47    body: String,
48}
49
50/// POST request with JSON body
51async fn create_github_issue(repo: String, title: String, body: String) -> Result<Value> {
52    let client = Client;
53    let url = format!("https://api.github.com/repos/{}/issues", repo);
54
55    let issue = CreateIssue { title, body };
56
57    let response = client
58        .post(url)
59        .header("Accept", "application/vnd.github.v3+json")
60        .header("Authorization", "token YOUR_GITHUB_TOKEN")
61        .json(&issue)?
62        .send()
63        .await?
64        .error_for_status()?;
65
66    response.json_value().await
67}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.