pub struct Response { /* private fields */ }Expand description
A response from a fetch request
Implementations§
Source§impl Response
impl Response
Sourcepub async fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T>
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}Sourcepub async fn json_value(&self) -> Result<Value>
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}Sourcepub fn error_for_status(self) -> Result<Self>
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§
impl Freeze for Response
impl RefUnwindSafe for Response
impl !Send for Response
impl !Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more