MCPcopy Create free account
hub / github.com/kataras/iris / postExample

Function postExample

_examples/compression/client-using-iris/main.go:66–112  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

64}
65
66func 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}

Callers 1

mainFunction · 0.70

Calls 6

NewCompressWriterFunction · 0.92
NewCompressReaderFunction · 0.92
EncodeMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.65
DoMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…