英文:
angular 8 Http POST with content type application/x-www-form-urlencoded
问题
似乎有点奇怪问这个问题,但我无法弄清楚。
我已经查看了stackoverflow上的链接以及谷歌上的一些东西,事实上尝试了很多方法,但我无法解决这个问题。以下是一些要提到的内容:
https://stackoverflow.com/questions/51912201/angular-6-http-client-post-bad-request
问题 - 对API的OPTIONS请求始终为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'); //这里的字母是出于安全原因而虚构的
// URL前面有https
return this.http.post('abc.com/authtoken', body, {headers: headers }).pipe(
map(res => {
return res;
})
);
现在,当我在另一个项目中尝试相同的代码时,它不会通过OPTIONS请求,而是抛出400(错误请求)。
我们可以假设其余的事情都已经按照要求进行了设置。问题主要出现在代码片段中。
最令人惊讶的是,当我在URL末尾添加斜杠时,比如 - authtoken/,它似乎会通过OPTIONS 200OK,但在原始的POST请求中失败,返回404,这是可以理解的,因为authtoken/不存在。
如何解决这个问题我有点困惑。
如我所说,我已经尝试了很多方法来使其工作。请提供建议。
我们正在使用Angular 8.2.13。
英文:
It seems to be a bit weird to ask this question, but i could not figure that out.
I have been through stackoverflow links on this and on google and infact tried many of the stuffs, but I could not get the resolution to this. Below are some to mention
https://stackoverflow.com/questions/51912201/angular-6-http-client-post-bad-request
PROBLEM - request OPTIONS for the api is always 400(bad request)
INSIGHTS -
Below is the code which is working on another project
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;
})
);
Now when I try the same code in another project, it doesnot pass OPTIONS request, throwing 400(Bad request).
we can assume that rest of the stuffs are in order to make this work. The problem mainly is in the code snippet.
The most amazing thing is that when I put a trailing slash in the url like - authtoken/ , it seem to pass the OPTIONS 200OK but fails in the original POST request as 404, quite understandable as the authtoken/ does not exist.
what is the way to go pass that. I am a bit confused as to do what.
As said, I have tried many of the stuffs to make it work. Please suggest
We are using Angular 8.2.13
Thanks
答案1
得分: 6
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 })
英文:
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 })
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论