Jump to content

Edit History

Jerome38190

Jerome38190


small precision

6 minutes ago, Nickz said:

more details get you more answers.

Hi !

To be honest it was really the same kind of code. But i just made it works! 

If some other people need to try sending a post request with image from node.js to prestashop, here is a working example:

const FormData = require("form-data");
const fs = require("fs");
const axios = require("axios");

// Read the image file into a buffer
const imageBuffer = fs.readFileSync("image.jpg");

// Create a FormData object
const form = new FormData();

// Append the image buffer to the form data
form.append("image", imageBuffer, "image.jpg");

// Make an HTTP POST request to the PrestaShop API
axios
  .post("https://example.com/api/images/products/15924", form, {
    params: {
      ws_key: "EXAMPLE",
    },
    headers: {
      ...form.getHeaders(),
      "Content-Length": form.getLengthSync(),
    },
  })
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.error(error.response.data);
  });

So, what was missing in my case (and in case of @vleungz one year ago) was to add to the headers the size with:

"Content-Length": form.getLengthSync()

It's also mandatory to have the filename as third parameter of the formData append function.

It was the 2 parts missing for me. Without theses, PHP don't receive anything in the $_FILES variable.

Hope that can helps some other!

Jerome38190

Jerome38190

5 minutes ago, Nickz said:

more details get you more answers.

Hi !

To be honest it was really the same kind of code. But i just made it works! 

If some other people need to try sending a post request with image from node.js to prestashop, here is a working example:

const FormData = require("form-data");
const fs = require("fs");
const axios = require("axios");

// Read the image file into a buffer
const imageBuffer = fs.readFileSync("image.jpg");

// Create a FormData object
const form = new FormData();

// Append the image buffer to the form data
form.append("image", imageBuffer, "image.jpg");

// Make an HTTP POST request to the PrestaShop API
axios
  .post("https://example.com/api/images/products/15924", form, {
    params: {
      ws_key: "EXAMPLE",
    },
    headers: {
      ...form.getHeaders(),
      "Content-Length": form.getLengthSync(),
    },
  })
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.error(error.response.data);
  });

So, what was missing in my case (and in case of @vleungz one year ago) was to add to the headers the size with:

"Content-Length": form.getLengthSync()

It's also mandatory to have the filename as third parameter of the formData append function.

It was the 2 parts missing for me.

Hope that can helps some other!

Jerome38190

Jerome38190

Just now, Nickz said:

more details get you more answers.

Hi !

To be honest it was really the same kind of code. But i just made it works ! 

If some other people need to try sending a post request with image from node.js to prestashop, here is a working example :

const FormData = require("form-data");
const fs = require("fs");
const axios = require("axios");

// Read the image file into a buffer
const imageBuffer = fs.readFileSync("image.jpg");

// Create a FormData object
const form = new FormData();

// Append the image buffer to the form data
form.append("image", imageBuffer, "image.jpg");

// Make an HTTP POST request to the PrestaShop API
axios
  .post("https://example.com/api/images/products/15924", form, {
    params: {
      ws_key: "EXAMPLE",
    },
    headers: {
      ...form.getHeaders(),
      "Content-Length": form.getLengthSync(),
    },
  })
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.error(error.response.data);
  });

So, what was missing in my case (and in case of @vleungz one year ago) was to add to the headers the size with

"Content-Length": form.getLengthSync()

It's also mandatory to have the filename as third parameter of the formData append function.

It was the 2 parts missing for me.

Hope that can helps some other !

×
×
  • Create New...