Struct Response

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

A response from a fetch request

Implementations§

Source§

impl Response

Source

pub fn status(&self) -> u16

Get the status code

Source

pub fn ok(&self) -> bool

Check if the response was successful (status 200-299)

Source

pub fn header(&self, name: &str) -> Result<Option<String>>

Get a header value

Source

pub async fn text(&self) -> Result<String>

Get the response body as text

Source

pub async fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T>

Get the response body as JSON

Examples found in repository?
examples/github_api.rs (line 25)
20async fn get_github_branch(repo: String) -> Result<GitHubBranch> {
21    let url = format!("https://api.github.com/repos/{}/branches/master", repo);
22
23    let response = get(&url).await?.error_for_status()?;
24
25    response.json().await
26}
27
28/// Advanced GET request with custom headers
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}
Source

pub async fn json_value(&self) -> Result<Value>

Get the response body as a dynamic JSON value

Examples found in repository?
examples/github_api.rs (line 66)
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 bytes(&self) -> Result<Vec<u8>>

Get the response body as bytes

Source

pub fn error_for_status(self) -> Result<Self>

Ensure the response was successful, returning an error if not

Examples found in repository?
examples/github_api.rs (line 23)
20async fn get_github_branch(repo: String) -> Result<GitHubBranch> {
21    let url = format!("https://api.github.com/repos/{}/branches/master", repo);
22
23    let response = get(&url).await?.error_for_status()?;
24
25    response.json().await
26}
27
28/// Advanced GET request with custom headers
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.