Currently init
accepts body
and the API uses JSON.stringify
to turn body
into request.data
. There should be the option to pass only the raw data
and bypass JSON stringification.
For example: API.post('api', '/endpoint', { data: "my raw text data" });
I have encountered an issue similar to this. Passing Content-Type': 'application/x-www-form-urlencoded
within the request header is not working to send payload data from the body
parameter directly. My code is as follows:
const apiOptions = {};
apiOptions['headers'] = {
'Content-Type': 'application/x-www-form-urlencoded'
};
apiOptions['body'] = 'foo=aaa&bar=bbb';
API.post('apiName', 'apiPath', apiOptions).then({ ... });
It just sends a request with 'Content-Type: application/json; charset=UTF-8'. Hopefully, the customized Content-Type
will be supported.
Same problem here with binary data - Content Type: application/octet-stream.
We would like to send binary data to API Gateway.
I think the fix here is quite straightforward. Something like this gets us 80% of the way there, and after two years I think even 80% would be amazing! :pray:
From b869b04c45924cd8932d70f16050bd7840217307 Mon Sep 17 00:00:00 2001
From: James Conroy-Finn <[email protected]>
Date: Thu, 10 Dec 2020 16:58:29 +0000
Subject: [PATCH] Not all APIs use JSON
---
packages/api-rest/src/RestClient.ts | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/packages/api-rest/src/RestClient.ts b/packages/api-rest/src/RestClient.ts
index d70b228b2..5843151fb 100644
--- a/packages/api-rest/src/RestClient.ts
+++ b/packages/api-rest/src/RestClient.ts
@@ -130,7 +130,17 @@ export class RestClient {
const initParams = Object.assign({}, init);
const isAllResponse = initParams.response;
- if (initParams.body) {
+
+ if (initParams.data && initParams.body) {
+ console.warn(
+ 'Use of data AND body in a single request is not supported. Data will be used and body ignored.',
+ initParams
+ );
+ }
+
+ if (initParams.data) {
+ params.data = initParams.data;
+ } else if (initParams.body) {
if (
typeof FormData === 'function' &&
initParams.body instanceof FormData
--
2.29.2
I hope someone on the Amplify team notices this. Not all APIs use JSON, and nor should they.
Update: I've just noticed this notice in the source of API
:
/**
* @deprecated
* Use RestApi or GraphQLAPI to reduce your application bundle size
* Export Cloud Logic APIs
*/
I can't see any mention of API
being deprecated in the docs however.
I plan on using the private _signed
function to workaround this JSON-encoded body limitation, and will share updates here if it goes well. :crossed_fingers:
Update 2: There's no easy way of modifying the way requests are built and run because of the way the code mixes building requests with IO. There's logic in there to fix clock skew and more that you'd lose by not following the exact code path that's in place. I think users of Amplify are essentially stuck with JSON or would need to handle request signing etc. themselves.
Most helpful comment
Same problem here with binary data - Content Type: application/octet-stream.
We would like to send binary data to API Gateway.