如何使用 swagger-client POST 二进制数据(八位字节流)
How to POST binary data (octet-stream) with swagger-client
我找不到学习如何使用这个东西发送二进制数据的资源。以下失败,状态为 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
})();
合约已打开api 3 并期望请求正文中有八位字节流:
...
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: ...
服务器在 swagger-ui 下按预期工作,所以问题不存在。
任何 help/resource 将不胜感激。
在 swagger-js github issues 中深入研究后,我发现在这种情况下还需要另一个输入:
let response = client.createWithBinary({/* params */}, {requestBody: data});
请求参数进入第一个参数; octet-stream 使用 requestBody
键进入第二个。
我找不到学习如何使用这个东西发送二进制数据的资源。以下失败,状态为 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
})();
合约已打开api 3 并期望请求正文中有八位字节流:
...
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: ...
服务器在 swagger-ui 下按预期工作,所以问题不存在。 任何 help/resource 将不胜感激。
在 swagger-js github issues 中深入研究后,我发现在这种情况下还需要另一个输入:
let response = client.createWithBinary({/* params */}, {requestBody: data});
请求参数进入第一个参数; octet-stream 使用 requestBody
键进入第二个。