egg
egg copied to clipboard
this.curl发起请求,无法带上自定义header
Your detail info about the Bug:
如下是一个调用OpenAI api的controller,请求OpenAI API接口的时候,自定义header带不上。 const Controller = require('egg').Controller;
class OpenAIController extends Controller {
async completions() {
const param = this.ctx.request.body;
const apiUrl = ${this.config.openai.baseUrl}/completions
;
try {
const result = await this.ctx.curl(apiUrl, {
method: 'POST',
contentType: 'json',
headers: {
Authorization: Bearer ${this.config.openai.apiKey}}
,
},
data: param,
streaming: param.stream,
dataType: 'json',
});
this.ctx.status = result.status;
this.ctx.set(result.header);
this.ctx.body = result.data;
} catch (error) {
this.ctx.status = 200;
this.ctx.body = { error: 'error' };
this.ctx.logger.info('OpenAI API call failed:', error);
}
}
}
module.exports = OpenAIController;
如下是抓包的结果:
从抓包的请求中可以看到,并没有自定义headers Authorization 这个请求头,这个是不符合预期的。
Reproduction Repo
Node Version
V16.20.1
Eggjs Version
3
Plugin Name and its version
无
Platform and its version
mac
注意:上述示例中的代码要运行,需要将config/config.default.js中的apikey字段设置成一个有效的apikey