我正在尝试使用正文和 header 执行发布请求。以下是我经历过的一些变化,但大多数情况下,我在服务器上收到错误消息,指出参数“key”未传入。
我在 postman 中尝试了这个 api,它在那里工作。这就是我在 Java/Spring Boot 中定义方法头的方式:
@RequestMapping(value = "/getIssue", method = RequestMethod.POST)
public IssuePojo getIssue(@RequestParam("key") String key, HttpServletRequest request) {
以下是我的 Angular 变化:
public getJiraIssue(key: string): Observable<any> {
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': this.idToken });
let options = { headers: headers };
const paramsA = new URLSearchParams();
paramsA.set('key', key);
let params = new HttpParams().append('key', key);
// return this.http.post(this.urlEnvironment.jiraGetIssue(), params, this.getHeaders());
console.log("headers: ", this.getHeaders());
// let obj = {
// key: key
// }
var headersA = new Headers();
headers.append('Authorization', this.idToken);
let body = new HttpParams()
.set('key', key);
return this.http.post(this.urlEnvironment.jiraGetIssue(), body, {headers: headers});
// return this.http.post(this.urlEnvironment.jiraGetIssue(), null, {headers: this.getHeaders(), params: params});
}
看来正文正在发送:
但这就是我得到的错误:
timestamp: "2019-01-30T04:30:40.721+0000", status: 400, error: "Bad Request",…}
error: "Bad Request"
message: "Required String parameter 'key' is not present"
path: "/jira/getIssue"
status: 400
timestamp: "2019-01-30T04:30:40.721+0000"
trace: "org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'key' is not present
最佳答案
HttpParams() 用于将查询字符串参数添加到请求 URL(如果需要),然后首先检查是否已将其添加到所发出的开发人员工具请求中,如果存在,则 api 端出现问题
关于带有正文和 header 的 Angular 7 post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54433364/