我正在尝试使用 reqwest 在 rust 中发出 HTTP 请求以从网站获取数据,类似于此命令将执行的操作
~$ curl -X POST -H "Content-Type: application/json" -d '{"video":""}' watchparty.me/createRoom -L
{"name":"/supreme-cherries-answer"}
~$
所以这是我尝试过的:
use reqwest::header;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Room {
name: String
}
async fn get_room_name() {
//new client with cookies
let client = reqwest::Client::builder()
.cookie_store(true)
.build()
.unwrap();
let body = r#"{
"video": ""
}"#;
let res = client
.post("https://watchparty.me/createRoom/")
.header(header::USER_AGENT, "mozilla/5.0")
.header(header::CONTENT_TYPE, "application/json")
.body(body)
.send()
.await
.unwrap();
match res.status() {
reqwest::StatusCode::OK => {
//make sure response is json and not an error
println!("Room name: {:?}", &res.text().await.unwrap());
},
_ => println!("Error: {}", res.status())
}
}
#[tokio::main]
async fn main() {
get_room_name().await;
}
事情是,有了这段代码,我得到了这个作为回应
Room name: "<!doctype html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\"/>
<link rel=\"icon\" href=\"/favicon.ico\"/>
<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"/>
<meta name=\"theme-color\" content=\"#000000\"/>
<meta name=\"description\" content=\"Watch together with friends. Chat and react in real time to the same stream, whether it's YouTube, your own video, or a virtual browser.\"/>
<meta property=\"og:image\" content=\"/screenshot4.png\"/>
<link rel=\"apple-touch-icon\" href=\"/favicon.png\"/>
<link rel=\"manifest\" href=\"/manifest.json\"/>
<title>WatchParty</title>
<script defer=\"defer\" src=\"/static/js/main.437dd445.js\"></script>
<link href=\"/static/css/main.9154545e.css\" rel=\"stylesheet\">
</head>
<body>
<script>!function(){var a=\"fbclid\";if(-1!==location.search.indexOf(\"fbclid=\")){var e=\"\";try{var c=new URL(location);c.searchParams.delete(a),e=c.href}catch(a){var l=new RegExp(\"[?&]fbclid=.*$\");e=location.search.replace(l,\"\"),e=location.pathname+e+location.hash}history.replaceState(null,\"\",e)}}()</script>
<noscript>You need to enable JavaScript to run this app.</noscript> <----- this is the part that annoys me
<div id=\"root\"></div>
</body>
<script async src=\"https://www.googletagmanager.com/gtag/js?id=UA-45337794-6\"></script><script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag(\"js\",new Date),gtag(\"config\",\"UA-45337794-6\")</script>
</html>
"
我不知道如何才能让我的代码得到与我的 curl 查询相同的结果(当然不使用 curl 箱子),有人有想法吗?