You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18-5Lines changed: 18 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ This repository contains a generic HTTP client which can be adapted to provide:
7
7
* Ability to send files and data of type `multipart/form-data`
8
8
* Ability to send data of type `application/x-www-form-urlencoded`
9
9
* Debugging capabilities to see the request and response data
10
-
* Streaming text events
10
+
* Streaming text and JSON events
11
11
12
12
API Documentation: https://pkg.go.dev/github.com/mutablelogic/go-client
13
13
@@ -159,6 +159,9 @@ modify each individual request when using the `Do` method:
159
159
*`OptTextStreamCallback(func(TextStreamCallback) error)` allows you to set a callback
160
160
function to process a streaming text response of type `text/event-stream`. See below for
161
161
more details.
162
+
*`OptJsonStreamCallback(func(any) error)` allows you to set a callback for JSON streaming
163
+
responses. The callback should have the signature `func(any) error`. See below for
164
+
more details.
162
165
163
166
## Authentication
164
167
@@ -191,9 +194,9 @@ You can also set the token on a per-request basis using the `OptToken` option in
191
194
192
195
You can create a payload with form data:
193
196
194
-
*`client.NewFormRequest(payload any, accept string)` returns a new request with a Form
197
+
*`client.NewFormRequest(payload any, accept string)` returns a new request with a Form
195
198
data payload which defaults to POST.
196
-
*`client.NewMultipartRequest(payload any, accept string)` returns a new request with
199
+
*`client.NewMultipartRequest(payload any, accept string)` returns a new request with
197
200
a Multipart Form data payload which defaults to POST. This is useful for file uploads.
198
201
199
202
The payload should be a `struct` where the fields are converted to form tuples. File uploads require a field of type `multipart.File`. For example,
@@ -241,9 +244,10 @@ type Unmarshaler interface {
241
244
}
242
245
```
243
246
244
-
## Streaming Responses
247
+
## Text Streaming Responses
245
248
246
-
The client implements a streaming text event callback which can be used to process a stream of text events, as per the [Mozilla specification](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events).
249
+
The client implements a streaming text event callback which can be used to process a stream of text events,
250
+
as per the [Mozilla specification](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events).
247
251
248
252
In order to process streamed events, pass the `OptTextStreamCallback()` option to the request
249
253
with a callback function, which should have the following signature:
@@ -272,3 +276,12 @@ If you return an error of type `io.EOF` from the callback, then the stream will
272
276
Similarly, if you return any other error the stream will be closed and the error returned.
273
277
274
278
Usually, you would pair this option with `OptNoTimeout` to prevent the request from timing out.
279
+
280
+
## JSON Streaming Responses
281
+
282
+
The client decodes JSON streaming responses by passing a callback function to the `OptJsonStreamCallback()` option.
283
+
The callback with signature `func(any) error` is called for each JSON object in the stream, where the argument
284
+
is the same type as the object in the request.
285
+
286
+
You can return an error from the callback to stop the stream and return the error, or return `io.EOF` to stop the stream
0 commit comments