ITADN

HeaderValue::from_str causes type mismatch error

#34Closedola-funnel 创建于 2024-10-02
O
ola-funnelcommented
Hello! I'm a beginner with Rust, so this might my own fault (and if it is, I'd love to get some pointers on what I did wrong), but I can't seem to get the basic example of the Request Builder `with_header` method to work. My code looks like below: ```rust use oxhttp::Client; use oxhttp::model::{Request, Method, Status, HeaderName, HeaderValue}; use std::str::FromStr; fn main() { let user_agent = "curl/8.7.1"; let client = Client::new().with_user_agent(user_agent).unwrap(); let url = "https://example.com/".parse().unwrap(); let request = Request::builder(Method::GET, url).with_header(HeaderName::ACCEPT, HeaderValue::from_str("text/html")).unwrap().build(); let response = client.request(request).unwrap(); assert_eq!(response.status(), Status::OK); println!("{}", response.header(&HeaderName::CONTENT_TYPE).unwrap()); let body = response.into_body().to_string().unwrap(); println!("{:?}", body); } ``` The above code causes the following error: ``` ola@R792Q6G924 changelog-scraper % cargo build Compiling changelog-scraper v0.1.0 (/Users/ola/workspace/rust/changelog-scraper) error[E0271]: type mismatch resolving `<Result<HeaderValue, InvalidHeader> as TryInto<HeaderValue>>::Error == InvalidHeader` --> src/main.rs:10:86 | 10 | let request = Request::builder(Method::GET, url).with_header(HeaderName::ACCEPT, HeaderValue::from_str("text/html")).unwrap().build(); | ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `InvalidHeader`, found `Infallible` | | | required by a bound introduced by this call | note: required by a bound in `RequestBuilder::with_header` --> /Users/ola/.cargo/registry/src/index.crates.io-6f17d22bba15001f/oxhttp-0.2.2/src/model/request.rs:126:42 | 123 | pub fn with_header( | ----------- required by a bound in this associated function ... 126 | value: impl TryInto<HeaderValue, Error = InvalidHeader>, | ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RequestBuilder::with_header` error[E0277]: the trait bound `HeaderValue: From<Result<HeaderValue, InvalidHeader>>` is not satisfied --> src/main.rs:10:86 | 10 | let request = Request::builder(Method::GET, url).with_header(HeaderName::ACCEPT, HeaderValue::from_str("text/html")).unwrap().build(); | ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Result<HeaderValue, InvalidHeader>>` is not implemented for `HeaderValue`, which is required by `Result<HeaderValue, InvalidHeader>: TryInto<HeaderValue>` | | | required by a bound introduced by this call | = note: required for `Result<HeaderValue, InvalidHeader>` to implement `Into<HeaderValue>` = note: required for `HeaderValue` to implement `TryFrom<Result<HeaderValue, InvalidHeader>>` = note: required for `Result<HeaderValue, InvalidHeader>` to implement `TryInto<HeaderValue>` note: required by a bound in `RequestBuilder::with_header` --> /Users/ola/.cargo/registry/src/index.crates.io-6f17d22bba15001f/oxhttp-0.2.2/src/model/request.rs:126:21 | 123 | pub fn with_header( | ----------- required by a bound in this associated function ... 126 | value: impl TryInto<HeaderValue, Error = InvalidHeader>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RequestBuilder::with_header` Some errors have detailed explanations: E0271, E0277. For more information about an error, try `rustc --explain E0271`. error: could not compile `changelog-scraper` (bin "changelog-scraper") due to 2 previous errors ``` I used the example from [here](https://docs.rs/oxhttp/0.2.2/oxhttp/model/struct.HeaderValue.html) as reference when writing the above code. Thanks in advance!
关闭于 2024-10-02 4 条评论