简体   繁体   中英

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.

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'

Then I saw that there are 2 Headers RequestVerificationToken and Cookie . I grabbed them and did a Postman request and got back a response with 200. But in Node.js when I do like

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 . 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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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