• rust学习-http-server端


    Get请求

    curl "http://127.0.0.1:8000/get/cat?task_id=123&demo=1"
    
    • 1

    Post请求

    curl 'http://localhost:8000/set/monkey' \
    -H "Content-Type:application/json" \
    -H 'Authorization:bearer' \
    -X POST \
    -d '{"name":"xiaoming", "age":12}'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    curl 'http://localhost:8000/set/monkey' \
    -H "Content-Type:application/json" \
    -H 'Authorization:bearer' \
    -X POST \
    -d '@/Users/xxx/test.json'
    
    • 1
    • 2
    • 3
    • 4
    • 5

    cat test.json

    {
       "name":"xiaoming", "age":12}
    
    • 1
    • 2

    代码示例

    // cat Cargo.toml
    [package]
    name = "rust_demo7"
    version = "0.1.0"
    edition = "2021"
    
    [dependencies]
    hyper = {
        version = "0.14", features = ["full"] }
    tokio = {
        version = "1", features = ["full"] }
    futures = "0.3"
    url = "2.2"
    serde = {
        version = "1.0", features = ["derive"] }
    serde_json = "1.0"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    // cat src/main.rs
    use hyper::{
       Server, Body, Method, Request, Response, StatusCode};
    use hyper::service::{
       make_service_fn, service_fn};
    use std::convert::Infallible;
    use std::net::SocketAddr;
    use serde::{
       Deserialize, Serialize};
    
    #[derive(Debug, Deserialize)]
    struct MyData {
       
        name: String,
        age: u32,
    }
    
    async fn handle_request(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
       
        match (req.method(), req.uri().path()) {
       
            (&Method::GET, "/watch/dog") => {
       
                print!("enter Get logic\n");
    
    			let params: Vec<(String, String)> = req
                    .uri()
                    .query()
                    .map(|query| {
       
                        url::form_urlencoded::parse(query.as_bytes())
                            .into_owned()
                            .collect()
                    })
                    .unwrap_or_else(Vec::new);
    
                // 打印参数
                for (key, value) in params {
       
                    println!("{} = {}", key, value);
                }
    
    			Ok(Response::new(Body::from("Hello, World!"
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
  • 相关阅读:
    Android Studio Error “Unsupported class file major version 61“---异常信息记录
    IDEA重装后打开的一些设置
    路由与交换技术-19-HSRP+PVSTP综合实验
    docker安装elasticsearch、kibana
    可视化—gojs 超多超实用经验分享(一)
    kafka详解二
    LeetCode:2. 两数相加
    ARM-A架构入门基础(三)MMU
    Android TextView自动缩放字体
    【Matplotlib绘制图像大全】(十七):散点图
  • 原文地址:https://blog.csdn.net/wangkai6666/article/details/133220265