Skip to content

Commit 118b1f8

Browse files
feat: Add support for sending JSON-encoded API requests
1 parent d7a4470 commit 118b1f8

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/client.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@ const axiosInstance = axios.create({
2626
*/
2727
interface SendRequestOptions {
2828
/**
29-
* Fields to include in message body (or params). Values must be either strings, or arrays of
30-
* strings (for repeated parameters).
29+
* Fields to include in message body (or params) as form-encoded data. Values must be either
30+
* strings, or arrays of strings (for repeated parameters).
31+
* Cannot be used together with jsonBody.
3132
*/
3233
data?: URLSearchParams;
34+
/**
35+
* JSON body to send with the request. If provided, Content-Type will be set to
36+
* application/json. Cannot be used together with data or fileBuffer.
37+
*/
38+
jsonBody?: Record<string, unknown>;
3339
/** Extra HTTP headers to include in request, in addition to headers defined in constructor. */
3440
headers?: Record<string, string>;
3541
/** Buffer containing file data to include. */
@@ -140,6 +146,12 @@ export class HttpClient {
140146
axiosRequestConfig.headers = {};
141147
}
142148
Object.assign(axiosRequestConfig.headers, form.getHeaders());
149+
} else if (options.jsonBody) {
150+
axiosRequestConfig.data = options.jsonBody;
151+
if (axiosRequestConfig.headers === undefined) {
152+
axiosRequestConfig.headers = {};
153+
}
154+
axiosRequestConfig.headers['Content-Type'] = 'application/json';
143155
} else if (options.data) {
144156
if (method === 'GET') {
145157
axiosRequestConfig.params = options.data;

0 commit comments

Comments
 (0)