# http

http库提供了各种常用的http功能，包括：服务端、客户端、websocket

## 作为客户端时：

* [func get](#get)
* [func getJson](#getJson)
* [func postForm](#postForm)
* [func postJson](#postJson)
* [type Request](#Request)

## 作为服务端时：

* [func createServe](#createServe)
* [func serve](#serve)
* [type RequestContext](#RequestContext)

## 函数文档

### func get(url, headers?)

用GET方法请求指定url的资源，返回一个Bytes值

若请求失败时，抛出异常

### func getJson(url, headers?)

用GET方法请求指定url的资源，并将返回内容按json格式解析为一个ZGG的值

若请求失败时，抛出异常

### func postForm(url, form, headers?)

以x-www-form-urlencoded方式往指定URL发起一次Post Form请求。若请求失败则抛出异常。

#### Examples

```
zgg> @http.postForm('https://httpbin.org/post', {
....   name: 'zgg',
....   values: [1, 2, 3],
.... }, {
....   Authorization: 'Bearer HelloWorld',
.... })
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "name": "zgg", 
    "values": [
      "1", 
      "2", 
      "3"
    ]
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Authorization": "Bearer HelloWorld", 
    "Content-Length": "35", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/2.0", 
    "X-Amzn-Trace-Id": "Root=1-62b404ea-7271f822152de246228bd973"
  }, 
  "json": null, 
  "origin": "137.59.101.43", 
  "url": "https://httpbin.org/post"
}
```

### func postJson(url, jsonValue, headers?)

往指定URL发起一次Post Json请求。若请求失败则抛出异常。

#### Examples

```
zgg> @http.postJson('https://httpbin.org/post', {
....   name: 'zgg',
....   values: [1, 2, 3],
.... }, {
....   'X-My-Header': 'My Value lalala',
.... })
{
  "args": {}, 
  "data": "{\"name\":\"zgg\",\"values\":[1,2,3]}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "31", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/2.0", 
    "X-Amzn-Trace-Id": "Root=1-62b40554-306f51a40a6b9ee901489c05", 
    "X-My-Header": "My Value lalala"
  }, 
  "json": {
    "name": "zgg", 
    "values": [
      1, 
      2, 
      3
    ]
  }, 
  "origin": "137.59.101.43", 
  "url": "https://httpbin.org/post"
}
```

### type Request

### func serve(listenAddr, handleFunc)

功能：启动一个web服务器，监听listenAddr地址（格式同go的http.ListenAndServe的第一个参数），并用handleFunc处理每个请求。

serve函数是一个非常简便的提供http服务的方法，适用于对外提供简单服务。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guanming.gitbook.io/zgg-doc/biao-zhun-ku/builtins/builtins_http.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
