I’m building a server-side action for a plugin I’m writing, in which I need send a POST request to an API.
The way I do it is, roughly like this:
function(properties, context) {
const fetch = require("node-fetch");
const body = {a: 1};
const result = fetch("https://some.random.api.com/v3.5/print/", {
method: "POST",
headers: {
Accept: "application/json",
Authorization: context.keys["API Key"],
"Content-Type": "application/x-www-form-urlencoded"
},
body: JSON.stringify(body)
}).then(response => console.log(response));
}
The request is constructed correctly. However, I can see this error in the server logs (ellipsis mine):
ERROR Unhandled Promise Rejection {“errorType”:“Runtime.UnhandledPromiseRejection”,“errorMessage”:“FetchError: request to https://some.random.api.com/v3.5/print/ failed, reason: Client network socket disconnected before secure TLS connection was established” …}
I’m a bit stuck. I guess I’m doing something fundamentally wrong here.