()
| 64 | } |
| 65 | |
| 66 | func postExample() { |
| 67 | buf := new(bytes.Buffer) |
| 68 | |
| 69 | // Compress client's data. |
| 70 | cw, err := context.NewCompressWriter(buf, encoding, -1) |
| 71 | if err != nil { |
| 72 | panic(err) |
| 73 | } |
| 74 | |
| 75 | json.NewEncoder(cw).Encode(payload{Username: "Edward"}) |
| 76 | |
| 77 | // `Close` or `Flush` required before `NewRequest` call. |
| 78 | cw.Close() |
| 79 | |
| 80 | endpoint := baseURL + "/" |
| 81 | |
| 82 | req, err := http.NewRequest(http.MethodPost, endpoint, buf) |
| 83 | if err != nil { |
| 84 | panic(err) |
| 85 | } |
| 86 | req.Header.Set("Content-Type", "application/json") |
| 87 | |
| 88 | // Required to send gzip compressed data to the server. |
| 89 | req.Header.Set("Content-Encoding", encoding) |
| 90 | // Required to receive server's compressed data. |
| 91 | req.Header.Set("Accept-Encoding", encoding) |
| 92 | |
| 93 | resp, err := client.Do(req) |
| 94 | if err != nil { |
| 95 | panic(err) |
| 96 | } |
| 97 | defer resp.Body.Close() |
| 98 | |
| 99 | // Decompress server's compressed reply. |
| 100 | cr, err := context.NewCompressReader(resp.Body, encoding) |
| 101 | if err != nil { |
| 102 | panic(err) |
| 103 | } |
| 104 | defer cr.Close() |
| 105 | |
| 106 | body, err := io.ReadAll(cr) |
| 107 | if err != nil { |
| 108 | panic(err) |
| 109 | } |
| 110 | |
| 111 | fmt.Printf("Server replied with: %s", string(body)) |
| 112 | } |
no test coverage detected
searching dependent graphs…