Angular代理将POST请求转为GET请求问题排查求助
问题
我需要向外部API发送POST请求,但代理会将所有POST请求转换为无请求头的GET请求。请问我的代理配置存在什么问题?
代理配置代码
const PROXY_CONFIG = { { "/api": { "target": "http://qa-2-api-workflow.runteccorp.com", "secure": false, "changeOrigin": true, "logLevel": "debug", "followRedirects": false, "httpVerbOverride": false, "pathRewrite": { "^/api": "/api/", } } } }; module.exports = PROXY_CONFIG;
HTTP请求代码
auth(user : string, password : string){ return this.http.post( //http = HttpClient "/api/login", { email: btoa(user), password: btoa(password)}, { observer: "response" } ). pipe(tap(res => { const authToken = res.body['access_token']; this.setToken(authToken); })); }
浏览器代理请求行为
(该路由预期接收POST而非GET)
分析与解决
你的代理配置存在两处语法错误,这会导致配置无法被正确解析,进而引发请求方法被异常转换的问题:
- 多余的外层大括号:
PROXY_CONFIG被定义为对象,但你额外嵌套了一层大括号,正确的结构应该直接将"/api"作为顶层属性。 - 尾逗号问题:
pathRewrite里的"^/api": "/api/",末尾多了一个逗号,在部分JS环境中会触发语法错误。
修正后的代理配置
const PROXY_CONFIG = { "/api": { "target": "http://qa-2-api-workflow.runteccorp.com", "secure": false, "changeOrigin": true, "logLevel": "debug", "followRedirects": false, "httpVerbOverride": false, "pathRewrite": { "^/api": "/api/" } } }; module.exports = PROXY_CONFIG;
修复语法错误后,代理工具会正确加载配置,按照预期转发POST请求,不会再将其转换为GET请求。如果问题仍然存在,可以查看logLevel: "debug"输出的日志,确认请求转发的具体细节。
内容的提问来源于stack exchange,提问作者Emeritus
相关产品推荐
相关产品推荐

