简体   繁体   English

无法在 Node.js 中向不属于我的 API 端点发出 axios post 请求

[英]Cannot make axios post request in Node.js to an API endpoint not owned by me

there is the website https://cebcare.ceb.lk/Incognito/DemandMgmtSchedule and in there I can see an API call to https://cebcare.ceb.lk/Incognito/GetLoadSheddingEvents with StartTime and EndTime as form data.有一个网站https://cebcare.ceb.lk/Incognito/DemandMgmtSchedule ,在那里我可以看到一个 API 调用https://cebcare.ceb.lk/Incognito/GetLoadSheddingEvents ,其中StartTimeEndTime作为表单数据。

I tried to send post request to above endpoint in Node.js using axios but I get the error AxiosError: unable to verify the first certificate and code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'我尝试使用 axios 将发布请求发送到 Node.js 中的上述端点,但出现错误AxiosError: unable to verify the first certificate and code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'

Then I saw that there are 2 Headers RequestVerificationToken and Cookie .然后我看到有 2 个 Headers RequestVerificationTokenCookie I grabbed them and did a Postman request and got back a response with 200. But in Node.js when I do like我抓住了它们并做了一个 Postman 请求并得到了 200 的响应。但是在 Node.js 中,当我喜欢

const data = {
  StartTime: startDate,
  EndTime: endDate,
};

const response = await axios.post(URL, data, {
  "Content-Type": "multipart/form-data",
  Cookie:
    ".AspNetCore.Antiforgery.ThOcTlhnrMo=CfDJ8Nr2EC612OFAjHvozOYXtlRQE9n05fuSOD0jEvKY0unmx8QyMYxdCfmotrhzVIKzurnhpkY_MtfAP9cmpR11u8rzt7_xz4IkuWMURwfelg7ymSJ8GaksLVwEgbMIkEDfrvjb5II6EzzTaLA5RiXRDXU",
  RequestVerificationToken:
    "CfDJ8Nr2EC612OFAjHvozOYXtlRBtAUjb36TUpOhI0yuLADjcckB_h1xKJWHDwl0MrqyE4_4pU_YXUkeh5uI66UBXedMcMmihENJ5hpfW_vBgNWZJ-JtliiE4UYvxNJCvvhmGvIWSKWeeqx-llCxrPio9Tw",
});

I get the same error as above我得到与上面相同的错误

Is there a way to fix this.有没有办法来解决这个问题。 Or somehow bypass the Cookie and RequestVerificationToken .或者以某种方式绕过CookieRequestVerificationToken Or can I hardcode these 2 values and send request?或者我可以硬编码这两个值并发送请求吗?

EDIT I did the following after looking at the linked post编辑我在查看链接后做了以下操作

const httpsAgent = new https.Agent({ rejectUnauthorized: false });

const response = await axios.post(URL, data, {
  httpsAgent,
  "Content-Type": "multipart/form-data",
  Cookie:
    ".AspNetCore.Antiforgery.ThOcTlhnrMo=CfDJ8Nr2EC612OFAjHvozOYXtlRQE9n05fuSOD0jEvKY0unmx8QyMYxdCfmotrhzVIKzurnhpkY_MtfAP9cmpR11u8rzt7_xz4IkuWMURwfelg7ymSJ8GaksLVwEgbMIkEDfrvjb5II6EzzTaLA5RiXRDXU",
  RequestVerificationToken:
    "CfDJ8Nr2EC612OFAjHvozOYXtlRBtAUjb36TUpOhI0yuLADjcckB_h1xKJWHDwl0MrqyE4_4pU_YXUkeh5uI66UBXedMcMmihENJ5hpfW_vBgNWZJ-JtliiE4UYvxNJCvvhmGvIWSKWeeqx-llCxrPio9Tw",
});

but now I'm getting Bad Request 400. But in postman Im getting the results for the same Cookie and RequestVerificationToken但现在我得到了 Bad Request 400。但是在邮递员中,我得到了相同CookieRequestVerificationToken的结果

The third parameter of the axios post function takes an "Options" object - as for your case to pass the headers you should write like this: axios post 函数的第三个参数采用“Options”对象——至于你传递标题的情况,你应该这样写:

{ 
   headers: {
                'Content-Type:'multipart/form-data',
                Cookie: "",
                RequestVerificationToken: ""
            } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM