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

method HEAD error #218

Open
clouds56 opened this issue Aug 17, 2020 · 11 comments · May be fixed by http-rs/http-client#97
Open

method HEAD error #218

clouds56 opened this issue Aug 17, 2020 · 11 comments · May be fixed by http-rs/http-client#97
Labels
bug Something isn't working

Comments

@clouds56
Copy link

clouds56 commented Aug 17, 2020

I tried to use http HEAD method and it would hand

  async_std::task::block_on(async move {
    let res = surf::head("http://example.com").await.expect("surf");
    println!("{:?}", res);
  });

And if i replace http with https, it would give me exception

thread 'test_header' panicked at 'surf: ResponseBodyError(None): unknown error', http/src/lib.rs:84:19

If I replace head with get, all things goes well.

[dependencies]
surf = "2.0.0-alpha.4"
async-std = "1.6.3"
@Fishrock123
Copy link
Member

Fishrock123 commented Aug 18, 2020

Could you try with this in your Cargo.toml?

[dependencies]
surf = { version = "2.0.0-alpha.4", features = ["h1-client"] }

@clouds56
Copy link
Author

Yes, I've tried

surf = { version = "2.0.0-alpha.4", features = [ "h1-client" , "middleware-logger", "encoding"], default-features = false }

but with no luck

@Fishrock123
Copy link
Member

@yoshuawuyts

@snaumov
Copy link

snaumov commented Nov 4, 2020

getting the same error with

surf = "2.1.0"
async-std = { version = "1.6.3" }

client.head("http://192.168.2.12:8080/file.car").recv_string().await?

@Fishrock123
Copy link
Member

Fishrock123 commented Nov 4, 2020

For what it's worth, this is a programmer error:

client.head("http://a.b").recv_string().await?;

It is a bug that this panics but will definitely continue to fail. HEAD cannot have a body, and so this will not work.
Use this instead, once this is fixed:

client.head("http://a.b").await?;

@Fishrock123 Fishrock123 added the bug Something isn't working label Nov 4, 2020
@austinabell
Copy link

For what it's worth, this is a programmer error:

client.head("http://a.b").recv_string().await?;

The docs should be updated when this is fixed as well, as we chatted about in discord, since the docs still show this as the usage:

/// let string = client.head("https://httpbin.org/head").recv_string().await?;

@Fishrock123
Copy link
Member

Fishrock123 commented Nov 4, 2020

Upon further investigation it seems like this is only an issue with the default curl-based client.

Try this:

surf = { version = "2.1", default-features = false, features = ["h1-client", "encoding"] }

Note that the async-h1 client is presently still missing a lot of client functionality, listed here:
#223 (comment)

The list I gathered:

Other things that may be less-than-complete:

  • redirects (by default?)
  • encodings (?)
  • TLS support (?)

You could also use "hyper-client" although that means opting in to all of hyper, and also using some tokio code.

Fishrock123 added a commit to Fishrock123/surf that referenced this issue Nov 4, 2020
@Fishrock123
Copy link
Member

Fishrock123 commented Nov 4, 2020

That being said, I can't reproduce the panic. For me this test just hangs forever. This machine is running Ubuntu 20 if that matters.

Can anyone get a bracktrace? cargo +nightly build and then run with RUST_BACKTRACE=1 in your ENV/.env.

@Fishrock123
Copy link
Member

Fishrock123 commented Nov 4, 2020

Ironically, CI running Ubuntu 20 does fail.

Error: ResponseBodyError(None): unknown error
thread 'head_example_org' panicked at 'assertion failed: `(left == right)`

@aswild
Copy link

aswild commented Jan 30, 2021

So... what is the correct way to do a basic HEAD request with surf? All of these return the same Err(ResponseBodyError(None)), both with surf version 2.1.0 and the tip of main (e30aa7c).

#[async_std::main]
async fn main() -> surf::Result<()> {
    let client = surf::client();
    let req = client.head("https://example.com");
    let resp = req.await;
    dbg!(&resp);

    let resp = surf::head("https://example.com").await;
    dbg!(&resp);

    let resp = surf::head("https://example.com").send().await;
    dbg!(&resp);

    Ok(())
}

What does work:

  • using the h1-client feature
  • using isahc directly isahc::head("https://example.com")
  • using curl directly: curl --head https://example.com

The error type ResponseBodyError seems to hint that surf is failing due to a missing response body, but HEAD isn't supposed to return a body.

@Fishrock123
Copy link
Member

Use the h1-client.

Fishrock123 added a commit to Fishrock123/http-client that referenced this issue Aug 30, 2021
If there is no body then don't construct a zero-lengthed body.

Fixes: http-rs/surf#321
Fixes: http-rs/surf#218
Fishrock123 added a commit to Fishrock123/http-client that referenced this issue Aug 30, 2021
If there is no body then don't construct a zero-lengthed body.

Fixes: http-rs/surf#321
Fixes: http-rs/surf#218
Fishrock123 added a commit to Fishrock123/http-client that referenced this issue Aug 30, 2021
If there is no body then don't construct a zero-lengthed body.

Fixes: http-rs/surf#321
Fixes: http-rs/surf#218
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants