alexcrichton / curl-rust

Rust bindings to libcurl
MIT License
1k stars 234 forks source link

Library with command line arguments always returns 301 #527

Closed GitCat3 closed 9 months ago

GitCat3 commented 9 months ago

I have the following code:

use curl::easy::Easy;
use std::env::args;

fn main() {
    let mut easy = Easy::new();
    let args: Vec<String> = args().collect();
    easy.url(&args[1]).unwrap();
    easy.perform().unwrap();
    println!("{}", easy.response_code().unwrap());
}

but when I run it no matter which website I pass into the command line, it always returns 301 for some reason. Is this an issue or did I do something wrong? Thanks!

UPDATE: I tried changing the code back by commenting out the lines that get command line arguments, and it still returned 301 after that for some reason.

ehuss commented 9 months ago

Curl does not follow redirects by default. You'll need to include easy.follow_location(true) to enable it.

GitCat3 commented 9 months ago

But shouldn't websites such as google.com not be saying I need to redirect anyway?

ehuss commented 9 months ago

google.com redirects to www.google.com. You can test these things on the command-line with curl -v https://google.com/.

GitCat3 commented 9 months ago

thanks, I was dumb.