【问题标题】:angular 8 Http POST with content type application/x-www-form-urlencodedangular 8 Http POST,内容类型为 application/x-www-form-urlencoded
【发布时间】:2020-04-21 23:59:30
【问题描述】:

问这个问题似乎有点奇怪,但我想不通。

我已经通过这个和谷歌上的 stackoverflow 链接,实际上尝试了很多东西,但我无法解决这个问题。下面是一些需要提及的

Angular 6 HTTP Client Post - Bad request

How to force Angular2 to POST using x-www-form-urlencoded

问题 - api 的请求选项始终为 400(错误请求)

洞察力 -

下面是另一个项目的代码

  const headers = new HttpHeaders({
    'Content-Type': 'application/x-www-form-urlencoded'
  });
  const body = new HttpParams()
    .set('grant_type', 'password')
    .set('username', stateData.username)
    .set('password', stateData.password)
    .set('client_id', 'XXXXX-5B3C-4A7D-WEW-23BWWWF9B7872'); //Letters are dummy here for security reasons

//URL preceded by https
return this.http.post('abc.com/authtoken', body , {headers: headers }).pipe(
  map(res => {
    return res;
  })
 );

现在当我在另一个项目中尝试相同的代码时,它没有通过 OPTIONS 请求,抛出 400(Bad request)。

我们可以假设其余的东西是为了完成这项工作。问题主要出在代码sn-p上。

最令人惊奇的是,当我在 url 中添加一个斜杠时,例如 - authtoken/ ,它似乎通过了 OPTIONS 200OK 但在原始 POST 请求中失败为 404,这是可以理解的,因为 authtoken/ 不存在。

通过它的方法是什么。我有点不知道该怎么做。

如前所述,我已经尝试了很多方法来让它发挥作用。请推荐

我们正在使用 Angular 8.2.13

谢谢

【问题讨论】:

  • 你有 URL 拦截器吗?如果是,请看一下,也许它会修改您的请求
  • @BELLIL :你是绝对正确的。我有用于加载和令牌服务的 HttpInterceptors。评论那些工作。现在的问题是如何让拦截器不修改请求?
  • 拦截器会自动修改所有请求,请分享您的 url 拦截器代码,以便我们看看它能做什么

标签: http-post angular8 x-www-form-urlencoded


【解决方案1】:
   const headers = new HttpHeaders().set(
     'Content-Type',
    'application/x-www-form-urlencoded;'
   );

   body = 'grant_type=' + password + '&username=' + setData.username + '&password=' + 
   setData.password + '&Client_Id=' + 'XXXXX-5B3C-4A7D-WEW-23BWWWF9B7872';

   return this.http.post('abc.com/authtoken', body , {headers: headers })

【讨论】:

    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2018-08-18
    • 2019-11-13
    • 2022-01-19
    • 2017-08-26
    相关资源
    最近更新 更多