#gostd #go-to-rust #go-standard-library #api-bindings #http

gostd_http

gostd_http is a part of gostd, gostd_http 是go标准库net/http的rust实现

5 stable releases

new 1.1.5 May 20, 2025
1.1.4 May 15, 2025
1.1.2 Apr 29, 2025

#16 in Accessibility

Download history 272/week @ 2025-04-28 36/week @ 2025-05-05 319/week @ 2025-05-12

627 downloads per month
Used in 3 crates (via gostd)

MIT license

365KB
7K SLoC

gostd_http 等价于 gostd::net::http

使用例子

http模块

Async 异步http

默认不启用异步方式

features =["async-std-runtime"] // 使用async_std 异步运行时
或者 features =["tokio-runtime"] // 使用 tokio 异步运行时

使用async_std

Cargo.toml配置:

async-std = {version = "1.13" ,features = ["attributes"]}
gostd = { version = "0.4" ,features =["async-std-runtime"]}
或者 gostd_http = { version = "1" ,features =["async-std-runtime"]}
  1. POST

use gostd::net::http::async_http; 
// 或者用 use gostd_http::async_http

#[async_std::main]
async fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet";
    let postbody = r#"{"id":0,"category":{"id":0,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}"#
   .as_bytes()
   .to_vec();
    let response = async_http::Post(url, "application/json", Some(postbody.into())).await?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error").to_vec()).unwrap()
    );

    Ok(())
}

或者

use gostd::net::http::{async_http::AsyncClient, Method, Request};
// 或者用 use gostd_http::{async_http::AsyncClient, Method, Request};
#[async_std::main]
async fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet";

    let postbody = r#"{
      "id": 0,
      "category": {
        "id": 0,
        "name": "string"
      },
      "name": "doggie",
      "photoUrls": [
        "string"
      ],
      "tags": [
        {
          "id": 0,
          "name": "string"
        }
      ],
      "status": "available"
    }"#
    .as_bytes()
    .to_vec();

    let mut req = Request::New(Method::Post, url, Some(postbody.into()))?;

    req.Header.Set("accept", "application/json");
    req.Header.Set("Content-Type", "application/json");
    let mut client = AsyncClient::New();
    let response = client.Do(&mut req).await?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error").to_vec()).unwrap()
    );

    Ok(())
}
// output
// {"id":92233723685477587,"category":{"id":,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":,"name":"string"}],"status":"available"}

  1. GET
use gostd::net::http::async_http;
// 或者用 use gostd_http::async_http;
#[async_std::main]
async fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet/findByStatus?status=available";
    let response = async_http::Get(url).await?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error").to_vec()).unwrap()
    );

    Ok(())
}

或者

use gostd::net::http::{async_http::AsyncClient, Method, Request};
// 或者用 use gostd_http::{async_http::AsyncClient, Method, Request};
#[async_std::main]
async fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet/findByStatus?status=available";
    let mut req = Request::New(Method::Get, url, None)?;
    req.Header.Set("Content-Type", "application/json");

    let mut client = AsyncClient::New();

    let response = client.Do(&mut req).await?;
    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error").to_vec()).unwrap()
    );

    Ok(())
}

使用tokio

Cargo.toml配置:

tokio = { version = "1.44", features = ["full"] }
gostd = { version = "0.4" ,features =["tokio-runtime""]}
或者 gostd_http = { version = "1" ,features =["tokio-runtime"]}
  1. POST

use gostd::net::http::async_http; 
// 或者用 use gostd_http::async_http;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet";
    let postbody = r#"{"id":0,"category":{"id":0,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}"#
   .as_bytes()
   .to_vec();
    let response = async_http::Post(url, "application/json", Some(postbody.into())).await?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error").to_vec()).unwrap()
    );

    Ok(())
}

或者

use gostd::net::http::{async_http::AsyncClient, Method, Request};
// 或者用 use gostd_http::{async_http::AsyncClient, Method, Request};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet";

    let postbody = r#"{
      "id": 0,
      "category": {
        "id": 0,
        "name": "string"
      },
      "name": "doggie",
      "photoUrls": [
        "string"
      ],
      "tags": [
        {
          "id": 0,
          "name": "string"
        }
      ],
      "status": "available"
    }"#
    .as_bytes()
    .to_vec();

    let mut req = Request::New(Method::Post, url, Some(postbody.into()))?;

    req.Header.Set("accept", "application/json");
    req.Header.Set("Content-Type", "application/json");
    let mut client = AsyncClient::New();
    let response = client.Do(&mut req).await?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error").to_vec()).unwrap()
    );

    Ok(())
}
// output
// {"id":92233723685477587,"category":{"id":,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":,"name":"string"}],"status":"available"}

  1. GET
use gostd::net::http::async_http;
// 或者用 use gostd_http::async_http;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet/findByStatus?status=available";
    let response = async_http::Get(url).await?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error").to_vec()).unwrap()
    );

    Ok(())
}

或者

use gostd::net::http::{async_http::AsyncClient, Method, Request};
// 或者用 use gostd_http::{async_http::AsyncClient, Method, Request};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet/findByStatus?status=available";
    let mut req = Request::New(Method::Get, url, None)?;
    req.Header.Set("Content-Type", "application/json");

    let mut client = AsyncClient::New();

    let response = client.Do(&mut req).await?;
    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error").to_vec()).unwrap()
    );

    Ok(())
}

Sync 同步http

client客户端

  1. POST

use gostd::net::http;
fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet";
    let postbody = r#"{"id":0,"category":{"id":0,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}"#
   .as_bytes()
   .to_vec();
    let response = http::Post(url, "application/json", Some(postbody))?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error")).unwrap()
    );

    Ok(())
}

或者

use gostd::net::http::{Client, Method, Request};

fn main() -> anyhow::Result<()> {

    let url = "https://petstore.swagger.io/v2/pet";

    let postbody = r#"{
      "id": 0,
      "category": {
        "id": 0,
        "name": "string"
      },
      "name": "doggie",
      "photoUrls": [
        "string"
      ],
      "tags": [
        {
          "id": 0,
          "name": "string"
        }
      ],
      "status": "available"
    }"#
    .as_bytes()
    .to_vec();

    let mut req = Request::New(Method::Post, url, Some(postbody))?;

    req.Header.Set("accept", "application/json");
    req.Header.Set("Content-Type", "application/json");
    let mut client = Client::New();
    let response = client.Do(&mut req)?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error")).unwrap()
    );

    Ok(())
}

// output
// {"id":92233723685477587,"category":{"id":,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":,"name":"string"}],"status":"available"}

  1. GET
use gostd::net::http;

fn main() -> anyhow::Result<()> {
    let url = "https://petstore.swagger.io/v2/pet/findByStatus?status=available";
    let response = http::Get(url)?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error")).unwrap()
    );

    Ok(())
}

或者

use gostd::net::http::{Client, Method, Request};

fn main() -> anyhow::Result<()> {

    let url = "https://petstore.swagger.io/v2/pet/findByStatus?status=available";
    let mut req = Request::New(Method::Get, url, None)?;
    req.Header.Set("Content-Type", "application/json");

    let mut client = Client::New();

    let response = client.Do(&mut req)?;
    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error")).unwrap()
    );

    Ok(())
}

multipart模块

form-data Body

use gostd::bytes;
use gostd::mime::multipart::Writer;
use gostd::net::http::{Client, Method, Request};
fn main() -> Result<(), std::io::Error> {
    let mut body = bytes::Buffer::new();
    let mut w = Writer::new(&mut body);
    w.WriteField("requestId", "12121231231")?;
    w.WriteField("testTime", "2022-01-22 18:00:00")?;
    w.WriteField("checkTime", "2022-01-22 22:00:00")?;
    w.WriteField("auditTime", "2022-01-22 23:00:00")?;
    w.WriteField("tubeCode", "QCGD99SDF")?;
    w.WriteField("testRatio", "1")?;
    w.WriteField("name", "刘xxx")?;
    w.WriteField("sex", "1")?;
    w.WriteField("birthdate", "20003-07-02")?;
    w.WriteField("address", "北京市丰台区")?;
    w.WriteField("phoneNumber", "1881xxxx")?;
    w.WriteField("cardType", "身份证")?;
    w.WriteField("cardNumber", "xxxx")?;
    w.WriteField("testResult", "0")?;
    w.WriteField("testUserName", "xxx")?;
    w.WriteField("checkUserName", "xxx")?;
    w.Close()?;
    let contentType = w.FormDataContentType();
    let url = "http://www.baidu.com";
    let mut req = Request::New(Method::Post, url, Some(body.Bytes()))?;
    req.Header.Set("Content-Type", contentType.as_str());
    let mut client = Client::New();
    let response = client.Do(&mut req)?;

    println!(
        "{}",
        String::from_utf8(response.Body.expect("return body error")).unwrap()
    );

    Ok(())
}

Dependencies

~10–21MB
~412K SLoC