-
Rust: reqwest GET/POST snippet컴퓨터/Rust 2021. 8. 19. 15:47728x90반응형
[dependencies] reqwest = { version = "0.11", features = ["json", "blocking"] } ...
GET
use reqwest::header::USER_AGENT; pub async fn get() -> Result<(), reqwest::Error> { let client = reqwest::Client::builder() .danger_accept_invalid_certs(true) .build()?; // USER_AGENT 설정 let res = client.get(URL).header(USER_AGENT, "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36").send().await?; let body = res.text().await?; // println!("Body:\n{}", body); Ok(()) }
POST
HarperDB에 insert nosql 명령어를 보내는 부분이다.
use reqwest::header::AUTHORIZATION; pub async fn post() -> Result<(), reqwest::Error> { let client = reqwest::Client::builder() .danger_accept_invalid_certs(true) .build()?; let payload = json!({"operation": "insert","schema": "ajou","table": "notice","records": [{"id": 1,"title": "Harper","date": "2021.08.17","link": "https:", "writer": "csw"}]}); // {"operation": "insert" ... } client .post(HARDER_URL) .header(AUTHORIZATION, HARDER_AUTH) .json(&payload) .send() .await?; Ok(()) }
728x90'컴퓨터 > Rust' 카테고리의 다른 글
Rust: actix + MongoDB (0) 2021.08.28 Rust: actix를 이용하여 카카오 챗봇 만들기 (1) 2021.08.02 Rust: scraper를 이용한 네이버 날씨 파싱 (0) 2021.07.28