@@ -149,7 +149,7 @@ use gostd::vendor
149149- [x] 完成mime::multipart模块。(version >=0.3.1)
150150- [x] 修复windos10平台编译失败的bug。(version>=0.3.18)
151151- [x] 对net/http模块,自定义错误处理上的优化,引入bytes提高性能,api会有参数类型的变化(version>=0.4.1)
152- - [ ] 对net/http模块,增加异步编程的支持,独立一个模块支持异步编程 ,原来模块保持不变。
152+ - [x ] 对net/http模块,增加异步编程的支持,独立一个模块async_http支持异步编程 ,原来模块保持不变。(version>=0.4.3)
153153- [x] net::http,net::url,io,strings,unicode,bytes 独立发布包。
154154
155155# 独立发布包
@@ -216,16 +216,270 @@ fn main() -> Result<(), std::io::Error> {
216216}
217217```
218218
219+
220+
219221## http模块
220222
223+ ### Async 异步http
224+ 默认不启用异步方式
225+
226+ features =["async-std-runtime"] // 使用async_std 异步运行时
227+ 或者 features =["tokio-runtime"] // 使用 tokio 异步运行时
228+
229+ #### 使用async_std
230+
231+ Cargo.toml配置:
232+
233+ async-std = {version = "1.13" ,features = ["attributes"]}
234+ gostd = { version = "0.4" ,features =["async-std-runtime"]}
235+
236+ 1 . POST
237+
238+ ``` rust
239+
240+ use gostd :: net :: http :: async_http
241+
242+ #[async_std :: main]
243+ async fn main() -> anyhow :: Result <()> {
244+ let url = "https: // petstore.swagger.io/v2/pet";
245+ let postbody = r#"{"id": 0,"category": {"id": 0,"name": "string"},"name": "doggie","photoUrls": ["string"],"tags": [{"id": 0,"name": "string"}],"status": "available"}"#
246+ . as_bytes()
247+ . to_vec();
248+ let response = async_http :: Post (url , " application/json" , Some (postbody . into ())). await ? ;
249+
250+ println! (
251+ " {}" ,
252+ String :: from_utf8 (response . Body . expect (" return body error" ). to_vec ()). unwrap ()
253+ );
254+
255+ Ok (())
256+ }
257+
258+ ```
259+ 或者
260+
261+ ``` rust
262+ use gostd :: net :: http :: {async_http :: AsyncClient , Method , Request };
263+
264+ #[async_std:: main]
265+ async fn main () -> anyhow :: Result <()> {
266+ let url = " https://petstore.swagger.io/v2/pet" ;
267+
268+ let postbody = r # " {
269+ "id": 0,
270+ "category": {
271+ "id": 0,
272+ "name": "string"
273+ },
274+ "name": "doggie",
275+ "photoUrls": [
276+ "string"
277+ ],
278+ "tags": [
279+ {
280+ "id": 0,
281+ "name": "string"
282+ }
283+ ],
284+ "status": "available"
285+ }" #
286+ . as_bytes ()
287+ . to_vec ();
288+
289+ let mut req = Request :: New (Method :: Post , url , Some (postbody . into ()))? ;
290+
291+ req . Header . Set (" accept" , " application/json" );
292+ req . Header . Set (" Content-Type" , " application/json" );
293+ let mut client = AsyncClient :: New ();
294+ let response = client . Do (& mut req ). await ? ;
295+
296+ println! (
297+ " {}" ,
298+ String :: from_utf8 (response . Body . expect (" return body error" ). to_vec ()). unwrap ()
299+ );
300+
301+ Ok (())
302+ }
303+ // output
304+ // {"id":92233723685477587,"category":{"id":,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":,"name":"string"}],"status":"available"}
305+
306+ ```
307+
308+ 2 . GET
309+
310+ ``` rust
311+ use gostd :: net :: http :: async_http;
312+
313+ #[async_std:: main]
314+ async fn main () -> anyhow :: Result <()> {
315+ let url = " https://petstore.swagger.io/v2/pet/findByStatus?status=available" ;
316+ let response = async_http :: Get (url ). await ? ;
317+
318+ println! (
319+ " {}" ,
320+ String :: from_utf8 (response . Body . expect (" return body error" ). to_vec ()). unwrap ()
321+ );
322+
323+ Ok (())
324+ }
325+
326+ ```
327+ 或者
328+
329+ ``` rust
330+ use gostd :: net :: http :: {async_http :: AsyncClient , Method , Request };
331+
332+ #[async_std:: main]
333+ async fn main () -> anyhow :: Result <()> {
334+ let url = " https://petstore.swagger.io/v2/pet/findByStatus?status=available" ;
335+ let mut req = Request :: New (Method :: Get , url , None )? ;
336+ req . Header . Set (" Content-Type" , " application/json" );
337+
338+ let mut client = AsyncClient :: New ();
339+
340+ let response = client . Do (& mut req ). await ? ;
341+ println! (
342+ " {}" ,
343+ String :: from_utf8 (response . Body . expect (" return body error" ). to_vec ()). unwrap ()
344+ );
345+
346+ Ok (())
347+ }
348+
349+ ```
350+
351+ #### 使用tokio
352+
353+ Cargo.toml配置:
354+
355+ tokio = { version = "1.44", features = ["full"] }
356+ gostd = { version = "0.4" ,features =["tokio-runtime""]}
357+
358+ 1 . POST
359+
360+ ``` rust
361+
362+ use gostd :: net :: http :: async_http
363+
364+ #[tokio :: main]
365+ async fn main() -> anyhow :: Result <()> {
366+ let url = "https: // petstore.swagger.io/v2/pet";
367+ let postbody = r#"{"id": 0,"category": {"id": 0,"name": "string"},"name": "doggie","photoUrls": ["string"],"tags": [{"id": 0,"name": "string"}],"status": "available"}"#
368+ . as_bytes()
369+ . to_vec();
370+ let response = async_http :: Post (url , " application/json" , Some (postbody . into ())). await ? ;
371+
372+ println! (
373+ " {}" ,
374+ String :: from_utf8 (response . Body . expect (" return body error" ). to_vec ()). unwrap ()
375+ );
376+
377+ Ok (())
378+ }
379+
380+ ```
381+ 或者
382+
383+ ``` rust
384+ use gostd :: net :: http :: {async_http :: AsyncClient , Method , Request };
385+
386+ #[tokio:: main]
387+ async fn main () -> anyhow :: Result <()> {
388+ let url = " https://petstore.swagger.io/v2/pet" ;
389+
390+ let postbody = r # " {
391+ "id": 0,
392+ "category": {
393+ "id": 0,
394+ "name": "string"
395+ },
396+ "name": "doggie",
397+ "photoUrls": [
398+ "string"
399+ ],
400+ "tags": [
401+ {
402+ "id": 0,
403+ "name": "string"
404+ }
405+ ],
406+ "status": "available"
407+ }" #
408+ . as_bytes ()
409+ . to_vec ();
410+
411+ let mut req = Request :: New (Method :: Post , url , Some (postbody . into ()))? ;
412+
413+ req . Header . Set (" accept" , " application/json" );
414+ req . Header . Set (" Content-Type" , " application/json" );
415+ let mut client = AsyncClient :: New ();
416+ let response = client . Do (& mut req ). await ? ;
417+
418+ println! (
419+ " {}" ,
420+ String :: from_utf8 (response . Body . expect (" return body error" ). to_vec ()). unwrap ()
421+ );
422+
423+ Ok (())
424+ }
425+ // output
426+ // {"id":92233723685477587,"category":{"id":,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":,"name":"string"}],"status":"available"}
427+
428+ ```
429+
430+ 2 . GET
431+
432+ ``` rust
433+ use gostd :: net :: http :: async_http;
434+
435+ #[tokio:: main]
436+ async fn main () -> anyhow :: Result <()> {
437+ let url = " https://petstore.swagger.io/v2/pet/findByStatus?status=available" ;
438+ let response = async_http :: Get (url ). await ? ;
439+
440+ println! (
441+ " {}" ,
442+ String :: from_utf8 (response . Body . expect (" return body error" ). to_vec ()). unwrap ()
443+ );
444+
445+ Ok (())
446+ }
447+
448+ ```
449+ 或者
450+
451+ ``` rust
452+ use gostd :: net :: http :: {async_http :: AsyncClient , Method , Request };
453+
454+ #[tokio:: main]
455+ async fn main () -> anyhow :: Result <()> {
456+ let url = " https://petstore.swagger.io/v2/pet/findByStatus?status=available" ;
457+ let mut req = Request :: New (Method :: Get , url , None )? ;
458+ req . Header . Set (" Content-Type" , " application/json" );
459+
460+ let mut client = AsyncClient :: New ();
461+
462+ let response = client . Do (& mut req ). await ? ;
463+ println! (
464+ " {}" ,
465+ String :: from_utf8 (response . Body . expect (" return body error" ). to_vec ()). unwrap ()
466+ );
467+
468+ Ok (())
469+ }
470+
471+ ```
472+
473+ ### Sync 同步http
474+
221475### client客户端
222476
2234771 . POST
224478
225479``` rust
226480
227481use gostd :: net :: http;
228- fn main () -> Result <(), std :: io :: Error > {
482+ fn main () -> anyhow :: Result <()> {
229483 let url = " https://petstore.swagger.io/v2/pet" ;
230484 let postbody = r # " {"id":0,"category":{"id":0,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}" #
231485 . as_bytes ()
@@ -246,7 +500,7 @@ fn main() -> Result<(), std::io::Error> {
246500``` rust
247501use gostd :: net :: http :: {Client , Method , Request };
248502
249- fn main () -> Result <(), std :: io :: Error > {
503+ fn main () -> anyhow :: Result <()> {
250504
251505 let url = " https://petstore.swagger.io/v2/pet" ;
252506
@@ -296,7 +550,7 @@ fn main() -> Result<(), std::io::Error> {
296550``` rust
297551use gostd :: net :: http;
298552
299- fn main () -> Result <(), std :: io :: Error > {
553+ fn main () -> anyhow :: Result <()> {
300554 let url = " https://petstore.swagger.io/v2/pet/findByStatus?status=available" ;
301555 let response = http :: Get (url )? ;
302556
@@ -314,7 +568,7 @@ fn main() -> Result<(), std::io::Error> {
314568``` rust
315569use gostd :: net :: http :: {Client , Method , Request };
316570
317- fn main () -> Result <(), std :: io :: Error > {
571+ fn main () -> anyhow :: Result <()> {
318572
319573 let url = " https://petstore.swagger.io/v2/pet/findByStatus?status=available" ;
320574 let mut req = Request :: New (Method :: Get , url , None )? ;
0 commit comments