Skip to content

Commit fa83cc2

Browse files
committed
Updated the documentation
1 parent eb403ce commit fa83cc2

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This repository contains a generic HTTP client which can be adapted to provide:
77
* Ability to send files and data of type `multipart/form-data`
88
* Ability to send data of type `application/x-www-form-urlencoded`
99
* Debugging capabilities to see the request and response data
10+
* Streaming JSON responses
1011

1112
Documentation: https://pkg.go.dev/github.com/mutablelogic/go-client/pkg/client
1213

@@ -70,9 +71,8 @@ Various options can be passed to the client `New` method to control its behaviou
7071
The first argument to the `Do` method is the payload to send to the server, when set. You can create a payload
7172
using the following methods:
7273

73-
* `client.NewRequest(accept string)` returns a new empty payload which defaults to GET. The accept parameter is the
74-
accepted mime-type of the response.
75-
* `client.NewJSONRequest(payload any, accept string)` returns a new request with a JSON payload which defaults to GET.
74+
* `client.NewRequest()` returns a new empty payload which defaults to GET.
75+
* `client.NewJSONRequest(payload any, accept string)` returns a new request with a JSON payload which defaults to POST.
7676
* `client.NewMultipartRequest(payload any, accept string)` returns a new request with a Multipart Form data payload which
7777
defaults to POST.
7878
* `client.NewFormRequest(payload any, accept string)` returns a new request with a Form data payload which defaults to POST.
@@ -98,7 +98,7 @@ func main() {
9898
Reply string `json:"reply"`
9999
}
100100
request.Prompt = "Hello, world!"
101-
payload := client.NewJSONRequest(request, "application/json")
101+
payload := client.NewJSONRequest(request)
102102
if err := c.Do(payload, &response, OptPath("test")); err != nil {
103103
// Handle error
104104
}
@@ -146,7 +146,9 @@ Various options can be passed to modify each individual request when using the `
146146
* `OptToken(value Token)` adds an authorization header (overrides the client OptReqToken option)
147147
* `OptQuery(value url.Values)` sets the query parameters to a request
148148
* `OptHeader(key, value string)` appends a custom header to the request
149-
149+
* `OptResponse(func() error)` allows you to set a callback function to process a streaming response.
150+
See below for more details.
151+
* `OptNoTimeout()` disables the timeout on the request, which is useful for long running requests
150152

151153
## Authentication
152154

@@ -183,3 +185,9 @@ You can create a payload with form data:
183185
* `client.NewMultipartRequest(payload any, accept string)` returns a new request with a Multipart Form data payload which defaults to POST. This is useful for file uploads.
184186

185187
The payload should be a `struct` where the fields are converted to form tuples. File uploads require a field of type `multipart.File`.
188+
189+
## Streaming Responses
190+
191+
If the returned content is a stream of JSON responses, then you can use the `OptResponse(fn func() error)` option, which
192+
will be called by the `Do` method for each response. The function should return an error if the stream should be terminated.
193+
Usually, you would pair this option with `OptNoTimeout` to prevent the request from timing out.

0 commit comments

Comments
 (0)