DevSight

Rust, async reqwest(request)

Rust 프로그램에서 HTTP, Rest API(webapi)를 사용하기 위해 reqwest 패키지를 활용한 기본적인 아래의 예제는 blocking 방식과 async로 확장한 방법을 보여준다.

예제에 필요한 기본적인 패키지는 reqwest 외에 tokio, serde를 사용한다. 예제에 사용한 베이스 코드는 Proful Sadangi(Youtube)1를 참고2하였다.

환경구성(vscode)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#Cargo.toml
[dependencies]
reqwest = { version = "0.11", features = ["blocking","json"] }
tokio = { version = "1.15.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.74"

#rustfmt.toml
max_width = 200
fn_args_layout = "Compressed"
use_small_heuristics = "Max"

#.cargo/config : static compile option
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
기본 Blocking 예제
Read More ···