angular - 如何在使用 Typescript 的 API Post 请求中使用 Body 参数
问题描述
我想知道如何使用打字稿将正文添加到 api 请求中。我已经在邮递员上完成了这个请求,我得到了回复,但是我不知道如何使用打字稿来完成。我不断收到“错误请求”。我检查了主机的 api 文档,他们向我展示了如何做到这一点:
Request Format
POST https://api.channeladvisor.com/oauth2/token
Authorization: Basic [application id:shared secret]
Content-Type: application/x-www-form-urlencoded
Body: grant_type = refresh_token &
refresh_token = [refresh token]
我有刷新令牌和授权详细信息。所以我尝试在打字稿中这样做:
this.http.post('https://api.channeladvisor.com/oauth2/token',
{body:{
grant_type:"refresh_token",
refresh_token:this.refresh_token
}},
{headers:{
'Authorization':this.token
}})
.subscribe((response)=>{
this.new_token=response;
console.log("This is the new token")
console.log(this.new_token)
})
}
但是当我运行它时,我得到一个错误的请求错误。我认为它与语法有关。
解决方案
你有以下身体要求:
grant_type = refresh_token &
refresh_token = [refresh token]
具有内容类型application/x-www-form-urlencoded
。
错误
你的代码:
this.http.post('https://api.channeladvisor.com/oauth2/token',
{body:{
grant_type:"refresh_token",
refresh_token:this.refresh_token
}},
将content-type
application/json
使用 json 格式的正文发送。
使固定
固定代码:
this.http.post('https://api.channeladvisor.com/oauth2/token',
{body:`
grant_type = refresh_token &
refresh_token = ${something}
`},
{headers:{
'Authorization':this.token,
'Content-Type':'application/x-www-form-urlencoded'
}})
推荐阅读
- r - 如何在图表中添加误差范围的阴影
- python - 在 Python 中查找函数 x = a*sin(x) 的所有根
- java - Spring在所有控制器方法中执行一个方法而不注入服务
- javascript - 最后两个数字前加逗号,三个数字后保留点
- javascript - javascript中基于对象的开关替代方案
- android - 为什么在我的 IDE 中我的缩进会像这样扩展我的代码(参数)
- bash - 如何使用 sudo 从另一个 bash 脚本运行 bash 脚本
- arrays - JMeter - 将文件转换为字符串到数组
- r - 如何计算大数据的行加权平均值?
- sql - 列出特定参考日期的一张表中的不匹配职位