使用HTTPSnippet生成Curl客户端代码时Header验证报错问题
解决HTTPSnippet生成Curl代码时的Headers验证错误
你的代码报错是因为HTTPSnippet对headers参数的格式要求是数组,不是你现在用的对象格式。按照它的规范,每个请求头都要拆成包含name和value的对象,再放进数组里。
修改后的正确代码如下:
const snippet = new HTTPSnippet({ method: 'GET', url: 'http://mockbin.com/request', headers: [ { name: 'content-type', value: 'Application/json' } ] }); const options = { indent: '\t' }; const output = snippet.convert('shell', 'curl', options); console.log(output);
执行这段代码后,就能生成包含指定Headers的Curl命令,输出类似:
curl "http://mockbin.com/request" \ -H "content-type: Application/json" \ -X GET
内容的提问来源于stack exchange,提问作者Amrinder Singh
相关产品推荐
相关产品推荐

