Skip to content

Commit

Permalink
feat(android): support passing custom RequestBody as content parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Mar 5, 2023
1 parent c4b7870 commit 4e5b513
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
28 changes: 17 additions & 11 deletions src/https/request.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,7 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
}
});
okHttpBody = builder.build();
if (opts.onProgress) {
okHttpBody = new com.nativescript.https.ProgressRequestWrapper(
okHttpBody,
new com.nativescript.https.ProgressRequestWrapper.ProgressListener({
onRequestProgress(bytesWritten: number, contentLength: number) {
opts.onProgress(bytesWritten, contentLength);
}
})
);
}

} else if (type === 'application/x-www-form-urlencoded') {
const builder = new okhttp3.FormBody.Builder();
Object.keys(opts.body).forEach((key) => {
Expand All @@ -503,7 +494,22 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
} else if (opts.content) {
body = opts.content;
}
okHttpBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse(type), body);
if (body instanceof okhttp3.RequestBody) {
okHttpBody = body;
} else {
okHttpBody = okhttp3.RequestBody.create(body, okhttp3.MediaType.parse(type));
}
}

if (opts.onProgress) {
okHttpBody = new com.nativescript.https.ProgressRequestWrapper(
okHttpBody,
new com.nativescript.https.ProgressRequestWrapper.ProgressListener({
onRequestProgress(bytesWritten: number, contentLength: number) {
opts.onProgress(bytesWritten, contentLength);
}
})
);
}
request[methods[opts.method]](okHttpBody);
}
Expand Down
5 changes: 4 additions & 1 deletion src/https/request.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export interface HttpsRequestOptions extends HttpRequestOptions {
headers?: Headers;
params?: HttpsRequestObject;
body?: HttpsRequestObject | HttpsFormDataParam[];
content?: string;
/**
* content can be used to pass native custom okhttp3.RequestBody
*/
content?: string | any;
/**
* Default 10 (seconds).
*/
Expand Down

0 comments on commit 4e5b513

Please sign in to comment.