How to POST binary data (octet-stream) with swagger-client

Viewed 579

I cannot find a resource to learn how to use this thing to send binary data. The following fails with status 415.

const fs = require('fs');
const SwaggerParser = require('swagger-parser');
const Swagger = require('swagger-client');

async function getClient() {
    let spec = await SwaggerParser.dereference('path/to/openapi.yaml')
    spec.servers = [{url: 'http://localhost:8080'}];
    return (await Swagger({spec})).apis.default;
}

(async function() {
    let client = getClient(); // client
    let data = fs.readFileSync('path/to/file.txt'); // buffer
    let response = client.createWithBinary(data) // <-- this is how we usually set req params and it fails 
})();

The contract is open api 3 and expects an octet-stream in the request body:

...
paths:
  /binary:
    post:
      operationId: createWithBinary
      description: Create a resource by uploading binary data
      requestBody:
        required: true
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
    responses:
      200:
        description: ...

The server works as expected with swagger-ui so the problem is not there. Any help/resource would be greatly appreciated.

1 Answers

After digging around in the swagger-js github issues, I've discovered that another input is needed in this case:

let response = client.createWithBinary({/* params */}, {requestBody: data});

Request params go inside the first arg; octet-stream goes in the second with a requestBody key.

Related