我想知道如何使用typescript将主体添加到api请求中。我已经在邮递员上做了这个请求,我得到了回应,但是我不知道如何使用typescript来做。我一直收到一个“糟糕的请求”。我查看了来自主机的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]我随身带着刷新令牌和授权详细信息。所以我试着在typescript中这样做:
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)
})
}但是当我运行它的时候,我得到了一个糟糕的请求错误。我认为这与语法有关。
发布于 2019-01-22 08:33:11
您需要满足以下主体要求:
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
}},将发送带有json格式正文的content-type application/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'
}})https://stackoverflow.com/questions/54295048
复制相似问题