Skip to content
Advertisement

Nodejs send post to admin-ajax.php in wordpress /getting 400 error and 0

i want to post to wordpress on nodejs using admin-ajax.php which set some actions to use. But i get statusCode:400 and 0, i don’t know so much about wordpress and ajax. I tried use a cookie from the worked project from wordpress that what i want to transfer nodejs How can i set up this?

const https = require("https");

const data = {
  action: "create_chapter",
  postID: "2676",
  chapterName: "2",
  chapterContent: "test",
};

const options = {
  hostname: "www.website.com",
  path: "/wp-admin/admin-ajax.php",
  method: "POST",
  headers: {
    "Cookie": "cookies",
    "X-Requested-With": "XMLHttpRequest",
  },
};

const req = https.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  res.on("data", (d) => {
    process.stdout.write(d);
  });
  
});
req.on("error", (error) => {
  console.error(error);
});

req.write(JSON.stringify(data));
req.end();

Advertisement

Answer

Latest Changes: added wordpress_logged_in to cookie, changed req.write to req.write(data); added content type: urlencoded and change data to url encoded

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement