我正在try 使用reqwest在RUST中发出一个HTTP请求,以从网站获取数据,这与此命令的作用类似
~$ curl -X POST -H "Content-Type: application/json" -d '{"video":""}' watchparty.me/createRoom -L
{"name":"/supreme-cherries-answer"}
~$
所以这是我try 过的:
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")
.header(header::ACCEPT, "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 crate ),有谁有主意吗?
100我对我的代码做了一些修改,修改了一些注释,但结果是相同的