post - 如何在 NestJS 中使用 HttpModule 发布表单数据
问题描述
我正在尝试从 Instagram API 获取访问令牌。我想将正文作为表单数据发送。这是我的代码。这是行不通的。但它在邮递员中工作正常。
async getAccessTokenByCode(code: string): Promise<string> {
const baseUrl = config.get('authorize.baseUrl');
const clientId = config.get('basic.clientId');
const clientSecret = config.get('basic.clientSecret');
const grantType = config.get('basic.grant_type');
const redirectUri = config.get('basic.redirect_uri');
const url = `${baseUrl}/oauth/access_token`
const form = new FormData();
form.append('client_id', clientId);
form.append('client_secret', clientSecret);
form.append('grant_type', grantType);
form.append('redirect_uri', redirectUri);
form.append('code', code);
try {
const response = await this.httpService.post(url, { data: form }, { headers: form.getHeaders() }).toPromise();
return response.data;
} catch (error) {
console.log(error);
}
}
如何使用 NestJS HttpModule 正确发送帖子数据?
解决方案
我不得不手动格式化数据。我不确定如何在 NestJS 中正确地做到这一点。例如,我还没有找到我会在 NodeJS 中使用的模块,例如 qs。
const data = `client_id=${clientId}&client_secret=${clientSecret}&grant_type=${grantType}&redirect_uri=${redirectUri}&code=${code}`;
推荐阅读
- reactjs - 我想在按下 react esc 时关闭模式
- database - InfluxDB 未启动:8086 绑定地址已在使用中
- r - 我在 r 中构建 knn 模型时遇到不同长度的错误
- sed - sed 从许多文件中删除一个特殊的控制字符
- python - 带有数据权重的 scipy.stats.mannwhitneyu 版本
- flutter - 如何在flutter上制作图标和文字翻译动画
- go - fyne GUI compile to Web Assembly failed
- javascript - 重新加载浏览器后,window.beforeunload 事件仍然在尝试存在页面时显示确认警报
- vue.js - Vuetify 数据表项。
未应用模板 - 但其他模板很好 - reactjs - 根据当前路径在 React Router 中设置活动链接