reqwest 请求示例

reqwest 请求示例:

  1. 读取图片
  2. 编码成base64
  3. 组装请求头、请求体,发起请求

返回值映射成HashMap:

use std::collections::HashMap;
use reqwest::header::HeaderMap;
use serde_json::value::Value;
use std::collections::hash_map::RandomState;
use reqwest::Error;

async fn do_post(url:&str,base64_str:&str) -> Result<HashMap<String, Value>, reqwest::Error>{
    let client = reqwest::Client::new();
    let mut headers = HeaderMap::new();
    headers.insert("Content-Type", "application/json".parse().unwrap());

    let mut data = HashMap::new();
    data.insert("file_content", base64_str);
    data.insert("channel_num", "CCTV");
    data.insert("file_id", "98997");
    data.insert("file_type", "jpg");
    data.insert("file_size", "45646");

    Ok(client.post(url)
    .headers(headers)
    .json(&data)
    .send()
    .await?
    .json::<HashMap<String, Value>>()
    .await?)
}


#[tokio::main]
async fn main() {
    let img_path = r"D:\cython\sanic_server\qianzi_pdf\other\0000.jpeg";
    let url = "http://192.168.140.131:5000/xxxx";
    let img_buffer = std::fs::read(img_path).unwrap();
    let base64_str = base64::encode(&img_buffer);
    if let Ok(res) = do_post(&url,&base64_str).await {
        println!("{:#?}", res);
    }
}

返回值映射成结构体:

[dependencies]
reqwest = { version = "0.10", features = ["json"] }
tokio = { version = "0.2", features = ["full"] }
base64="0.13.0"

serde = "*"
serde_derive = "*"
serde_json = "1.0"
use std::collections::HashMap;
use reqwest::header::HeaderMap;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;

async fn do_post(url:&str,base64_str:&str) -> Result<ResponseJson, reqwest::Error>{
    let client = reqwest::Client::new();
    let mut headers = HeaderMap::new();
    headers.insert("Content-Type", "application/json".parse().unwrap());

    let mut data = HashMap::new();
    data.insert("file_content", base64_str);
    data.insert("channel_num", "CCTV");
    data.insert("file_id", "98997");
    data.insert("file_type", "jog");
    data.insert("file_size", "45646");

    Ok(client
    .post(url)
    .headers(headers)
    .json(&data)
    .send()
    .await?
    .json::<ResponseJson>()
    .await?)
}

#[derive(Deserialize)]
struct RecogResult {
    insured:String,
    policyholder:String
}


#[derive(Deserialize)]
struct ResponseJson {
    request_id:String,
    code:i32,
    file_id:String,
    recog_result:RecogResult,
    cost_time:i32,
}


#[tokio::main]
async fn main() {
    let img_path = r"D:\cython\sanic_server\qianzi_pdf\other\0000.jpeg";
    let url = "http://192.168.140.131:5000/xxxx";
    let img_buffer = std::fs::read(img_path).unwrap();
    let base64_str = base64::encode(&img_buffer);

    match do_post(&url,&base64_str).await {
        Ok(response_json) => {
            let value: ResponseJson = response_json;
            println!("shuchu: cost_time={}",value.cost_time);
            println!("shuchu: code={}",value.code);
            println!("shuchu: file_id={}",value.file_id);
            println!("shuchu: insured={}",value.recog_result.insured);
            println!("shuchu: policyholder={}",value.recog_result.policyholder);
            println!("shuchu: request_id={}",value.request_id);
        }
        Err(_) => {}
    }
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页