Jump to content
  • 0

NodeJs / NestJS wysłanie obrazka na preste


zrealowanydawidos

Question

Cześć, mam problem z dodaniem obrazka na preste. Pobieram go z frontu zapisuje lokalnie na serwerze i próbuje go wysłać na preste. 

Udało mi się to zrobić za pomocą programu insomnia i następujących danych: 

https://klucz_API@nazwa_domeny.pl/api/images/categories/139 i w body wybieram multipart dodaje pole o naziwe "image" i zdjęcie, po czym presta zwraca mi 200 i samo zdjęcie. 

Teraz chcę zrobić to w NodeJs używając frameworka NestJs. 

  async addAvatar(id: number, photo: any) {
    const fs = require('fs');
    const FormData = require('form-data');

    const formData = new FormData();
    console.log('photo path', photo);
    formData.append(
      '--X-INSOMNIA-BOUNDARY Content-Disposition: form-data; name="image"; filename="1.png" Content-Type: image/png �PNG',
      fs.createReadStream('/home/dawid/Pulpit/Rolnik BE/platforma-rolnika-back/store/farm-photos/1.png'),
      '--X-INSOMNIA-BOUNDARY',
    );
    const url = 'https://[email protected]/api/images/categories/138';

    const request_config = {
      headers: {
        'Content-Type': 'multipart/form-data; boundary=X-INSOMNIA-BOUNDARY',
      },
      data: formData,
    };
    axios.interceptors.request.use(
      function(config) {
        // Do something before request is sent
        console.log('here request ---------------------->', config);
        return config;
      },
      function(error) {
        // Do something with request error
        return Promise.reject(error);
      },
    );

    try {
      let res = await axios.post(url, formData, request_config);
    } catch (error) {
      //  console.log("error", error)
      console.log('error 2 ', error.response.data);
      throw error;
    }
  }

Ten lepiony formData to na przykładzie Timeline z Insomni jest, próba odwzorowania. 

 

Całe zapytanie Http wygląda tak:
 

{ url:
   'https://[email protected]/api/images/categories/138',
  method: 'post',
  data:
   FormData {
     _overheadLength: 273,
     _valueLength: 0,
     _valuesToMeasure: [ [ReadStream] ],
     writable: false,
     readable: true,
     dataSize: 0,
     maxDataSize: 2097152,
     pauseStreams: true,
     _released: false,
     _streams:
      [ '----------------------------698062111955601001821892\r\nContent-Disposition: form-data; name="--X-INSOMNIA-BOUNDARY Content-Disposition: form-data; name="image"; filename="1.png" Content-Type: image/png �PNG"; filename="--X-INSOMNIA-BOUNDARY"\r\nContent-Type: image/png\r\n\r\n',
        [DelayedStream],
        [Function: bound ] ],
     _currentStream: null,
     _insideLoop: false,
     _pendingNext: false,
     _boundary: '--------------------------698062111955601001821892' },
  headers:
   { common: { Accept: 'application/json, text/plain, */*' },
     delete: {},
     get: {},
     head: {},
     post: { 'Content-Type': 'application/x-www-form-urlencoded' },
     put: { 'Content-Type': 'application/x-www-form-urlencoded' },
     patch: { 'Content-Type': 'application/x-www-form-urlencoded' },
     'Content-Type': 'multipart/form-data; boundary=X-INSOMNIA-BOUNDARY' },
  transformRequest: [ [Function: transformRequest] ],
  transformResponse: [ [Function: transformResponse] ],
  timeout: 0,
  adapter: [Function: httpAdapter],
  xsrfCookieName: 'XSRF-TOKEN',
  xsrfHeaderName: 'X-XSRF-TOKEN',
  maxContentLength: -1,
  maxBodyLength: -1,
  validateStatus: [Function: validateStatus] }

A odpowiedź prestashop:

 

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[66]]></code>
<message><![CDATA[Unable to save this image]]></message>
</error>
</errors>
</prestashop>

Ktoś jest w stanie mi napisać jak powinien wyglądać nagłówek http, oraz w jaki sposób na być wysyłane zdjęcie?

Edited by zrealowanydawidos (see edit history)
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...