0

I'm testing the self hosted parse-server-example and I want to request an image and save it on my server file system. My code so far:

Parse.Cloud.define('getImage', function(request, response) {
  
  var fs = require('fs');
  var testURL = 'http://example.com/test.jpg';

  Parse.Cloud.httpRequest({
    url: testURL,
    method: 'GET',
    success: function(httpResponse) {
      
      var imageBuffer = httpResponse.buffer;
      fs.writeFile("/mypath/processed.jpg", imageBuffer, function() {
        console.log('Saved to disk');
      });

      response.success('Saved image');
    },
    error: function(httpResponse) {
      response.error('Error getting image');
    }
  });
});

Problem: A file is created but is nearly doubled in size and can't be opened as an image file. It seems that httpResponse.buffer is returning gibberish.

I also tested to pass imageBuffer.toString('base64') as a parameter to writeFile but had no success. I think its just a simple misunderstanding on my side on how to process the image buffer to write it as a jpg file. Can somebody help or direct me to a working example?

baal
  • 257
  • 3
  • 12

1 Answers1

1

Found a work around, as it seems that Parse.Cloud.httpRequest ist not working correctly for a self hosted parse server right now.

I just used require('request'):

var httpRequest = require('request');
var fs = require('fs'); 

var url = 'http://www.example.com/test.jpg';

// simple HTTP GET request for the image URL
httpRequest.get({url: url, encoding: 'binary'}, function (err, httpResponse, body) {
  
  fs.writeFile('/mypath/processed.jpg', body, 'binary', function(err) {
    if(err) { 
      console.log('Error: '+err);
    } else {  
      console.log('Saved image');
    }
  }); 
});

This worked for me, but I would be glad to know why Parse.Cloud.httpRequest does not work?

baal
  • 257
  • 3
  • 12