Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set the user-agent on wasm target #2016

Closed
Tracked by #5670
FirelightFlagboy opened this issue Oct 30, 2023 · 2 comments · Fixed by #2018
Closed
Tracked by #5670

Allow to set the user-agent on wasm target #2016

FirelightFlagboy opened this issue Oct 30, 2023 · 2 comments · Fixed by #2018
Labels
E-easy Effort: Easy! Start here :D E-pr-welcome The feature is welcome to be added, instruction should be found in the issue.

Comments

@FirelightFlagboy
Copy link
Contributor

Currently reqwest provide the method ClientBuilder::user_agent(..) to set the User-Agent header.

But this method does not exist when targeting wasm32-unknown-unknown.

Steps to reproduce

I've the following src/lib.rs:

use reqwest::ClientBuilder;

pub async fn test() {
    let client = ClientBuilder::default()
        .user_agent("FooBar/1.2.3")
        .build()
        .unwrap();

    let _rep = client.get("https://www.google.com").send().await.unwrap();
}

and the following Cargo.toml:

[package]
name = "request-user-agent-wasm"
version = "0.1.0"
edition = "2021"

[dependencies]
reqwest = "0.11.22"

When I execute cargo check --target wasm32-unknown-unknown, I get the following error:

    Checking request-user-agent-wasm v0.1.0 (/tmp/request-user-agent-wasm)
error[E0599]: no method named `user_agent` found for struct `ClientBuilder` in the current scope
 --> src/lib.rs:5:10
  |
4 |       let client = ClientBuilder::default()
  |  __________________-
5 | |         .user_agent("FooBar/1.2.3")
  | |         -^^^^^^^^^^ method not found in `ClientBuilder`
  | |_________|
  | 

For more information about this error, try `rustc --explain E0599`.
error: could not compile `request-user-agent-wasm` (lib) due to previous error

What I'm expecting

I would like that reqwest::wasm::ClientBuilder provide the method user_agent(..) to set the User-Agent header

Current workaround

reqwest::wasm::ClientBuilder provide default_headers(..) so I can use it to set User-Agent

--- src/lib.rs.back	2023-10-30 15:59:03.729542547 +0100
+++ src/lib.rs	2023-10-30 16:01:03.162871429 +0100
@@ -1,11 +1,14 @@
-use reqwest::ClientBuilder;
+use reqwest::{header, ClientBuilder};
 
 pub async fn test() {
+    let headers = header::HeaderMap::from_iter([(
+        header::HeaderName::from_static("User-Agent"),
+        header::HeaderValue::from_static("FooBar/1.2.3"),
+    )]);
     let client = ClientBuilder::default()
-        .user_agent("FooBar/1.2.3")
+        .default_headers(headers)
         .build()
         .unwrap();
 
     let _rep = client.get("https://www.google.com").send().await.unwrap();
 }
-
@seanmonstar
Copy link
Owner

It should be a small addition to make this available. Would you like to submit a PR?

@seanmonstar seanmonstar added E-easy Effort: Easy! Start here :D E-pr-welcome The feature is welcome to be added, instruction should be found in the issue. labels Oct 30, 2023
@FirelightFlagboy
Copy link
Contributor Author

Yes, I'm already working on a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Effort: Easy! Start here :D E-pr-welcome The feature is welcome to be added, instruction should be found in the issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants