首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Typescript在API Post请求中使用Body参数

如何使用Typescript在API Post请求中使用Body参数
EN

Stack Overflow用户
提问于 2019-01-22 01:29:27
回答 1查看 1.7K关注 0票数 1

我想知道如何使用typescript将主体添加到api请求中。我已经在邮递员上做了这个请求,我得到了回应,但是我不知道如何使用typescript来做。我一直收到一个“糟糕的请求”。我查看了来自主机的api文档,他们告诉我如何做到这一点:

代码语言:javascript
复制
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中这样做:

代码语言:javascript
复制
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)
  })
}

但是当我运行它的时候,我得到了一个糟糕的请求错误。我认为这与语法有关。

EN

回答 1

Stack Overflow用户

发布于 2019-01-22 08:33:11

您需要满足以下主体要求:

代码语言:javascript
复制
  grant_type = refresh_token &
  refresh_token = [refresh token]

内容类型为application/x-www-form-urlencoded

错误

你的代码:

代码语言:javascript
复制
this.http.post('https://api.channeladvisor.com/oauth2/token',                    
  {body:{
    grant_type:"refresh_token",
    refresh_token:this.refresh_token
  }},

将发送带有json格式正文的content-type application/json

修复

修复代码:

代码语言:javascript
复制
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'
    }})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54295048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档