gaxios icon indicating copy to clipboard operation
gaxios copied to clipboard

[EDIT] Support POSTing binary data to another API using gaxios

Open arjunk-pki opened this issue 5 years ago • 3 comments

I'm using gaxios NPM library. I've an external service that accepts application/octet-stream as media type. I need to send a binary data from my Node.JS application to this server. How to do this ?. Whenever I try to send a binary data by converting it to Buffer, that external service throws media type application/json is not supported error. I think, gaxios internally serializes body into application/json. Please see my following code snippet.

const bufferData = Buffer.from(jsonObject.data)

I have to send this binary data using gaxios.

public async postBinaryData(data:Buffer) : Promise<any>{
   const requestOptions: GaxiosOptions = {};
   requestOptions.url = 'http://localhost/api/acceptBinary'; // External Service
   requestOptions.method = 'POST';
   requestOptions.headers['Content-Type'] = 'application/octet-stream'; // Should we add this ? It's not overriding default request mime type.
   const gaxiosResponse = await request(requestOptions);
}

When I used this, I got the below WARN.

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported]

arjunk-pki avatar Oct 12 '20 11:10 arjunk-pki

I have the same question. Two years in development and no answer :/ No idea how to send files using gaxios.

mpiorowski avatar May 07 '22 22:05 mpiorowski

I'm updated this to reflect the fact that we don't support this functionality today, and should potentially consider adding this feature as an improvement to the library.

bcoe avatar Jun 10 '22 18:06 bcoe

I might have misunderstood the issue, but I got it working ok for me.

I used:

...
data: fs.createReadStream(filename),
headers: {
  'Content-Type' : 'application/octet-stream'
}
...

as explained in the README file (see f8442221c9e107cddb78122707e1e75f5d0e6b05)

velsietis avatar Oct 07 '22 10:10 velsietis