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?

1 Answer 1

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.