Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.curl发起请求,无法带上自定义header #5242

Open
steelli opened this issue Jul 23, 2023 · 1 comment
Open

this.curl发起请求,无法带上自定义header #5242

steelli opened this issue Jul 23, 2023 · 1 comment

Comments

@steelli
Copy link

steelli commented Jul 23, 2023

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;

如下是抓包的结果:
image
从抓包的请求中可以看到,并没有自定义headers Authorization 这个请求头,这个是不符合预期的。

Reproduction Repo

归档.zip

Node Version

V16.20.1

Eggjs Version

3

Plugin Name and its version

Platform and its version

mac

@steelli
Copy link
Author

steelli commented Jul 23, 2023

注意:上述示例中的代码要运行,需要将config/config.default.js中的apikey字段设置成一个有效的apikey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant