this.curl发起请求,无法带上自定义header
- Dominant language
- TypeScript
- Stars
- 19k
- Forks
- 1.8k
- PR merge metrics
- No merged PRs in 30d
Description
### 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
[归档.zip](https://github.com/eggjs/egg/files/12138656/default.zip)
### Node Version
V16.20.1
### Eggjs Version
3
### Plugin Name and its version
无
### Platform and its version
mac
Contributor guide
Assessment
This issue has not been assessed yet.