首页 > 解决方案 > 我如何使用 Angular 9 中的 api 的 post 方法执行 http 请求

问题描述

这是我第一次尝试这样做,我不知道如何开始,如果有人可以指导我,那将非常有帮助。我正在尝试登录,如果我想让它工作,我必须使用 API 的 post 方法发出 http 请求,并将返回我需要的信息,但是当我尝试说带有 URL 的错误 404 时,我尝试了并Postman说我将在下面给出的其他错误。我不是说必须有人为我做,只是需要有人告诉我如何开始

Postman说的错误

{
    "type": "error",
    "message": "please make sure your meet the following field requirements",
    "check": 0,
    "passed": false,
    "requirements": [
        [
            "Identifier",
            {
                "is_required": true,
                "requires_value": true,
                "min_length": 1,
                "max_length": 150
            }
        ],
        [
            "Password",
            {
                "is_required": true,
                "requires_value": true,
                "min_length": 1,
                "max_length": 60
            }
        ]
    ]
}

标签: angularapihttprequestpostman

解决方案


这是一个执行发布请求的示例代码

let headers = new Headers({'Content-Type': 'application/json'});  
headers.append('Authorization','Bearer <your auth here>')
let options = new RequestOptions({headers: headers});
const body = { username, password }; // will depend on how your api will accept
 return this.http.post(APIname,body,options)
  .map(this.extractData)
  .catch(this.handleError);

推荐阅读