Hi there,
I’ve been scratching my head for a whole day trying to understand this. I’m testing using Moralis.Cloud.httpRequest function in a cloud function to retrieve a jpeg file from a website using the following code:
Moralis.Cloud.define("fetchData", async (request) => {
const logger = Moralis.Cloud.getLogger();
return Moralis.Cloud.httpRequest({
method: "GET",
url: request.params.theUrl,
}).then(function(httpResponse) {
logger.info(httpResponse);
return httpResponse;
}, function(httpResponse) {
logger.error('Request failed with response code ' + httpResponse.status);
});
});
I’ve feed it with just a link of the random jpeg online (so i know the data works). but the data in the httpResponse is all weird and such and don’t look like binary at all:
Result: {“status”:200,“headers”:{“content-type”:“image/jpeg”,“content-length”:“125966”,“connection”:“close”,“date”:“Wed, 20 Jul 2022 00:19:56 GMT”,“last-modified”:“Tue, 14 Nov 2017 18:45:22 GMT”,“etag”:"“13f01d01b5f356709254f36925145a5d”",“x-amz-version-id”:"_C042mLKEmLupO2ENS4kINrcphlKKDdy",“accept-ranges”:“bytes”,“server”:“AmazonS3”,“x-cache”:“Hit from cloudfront”,“via”:“1.1 baaa01540e8048678da317f40119ee06.cloudfront.net (CloudFront)”,“x-amz-cf-pop”:“MRS52-P4”,“x-amz-cf-id”:“SK7kbyAwRxVeOLbEiFH4KDGushTzsiLowwaNECqpuZMqdYf-QziyDQ==”,“age”:“211735”},“buffer”:{“type”:“Buffer”,“data”:[]},“text”:"����\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0001,\u0001,\u0000\u0000��\fXICC_PROFILE\u0000\u0001\u0001\u0000\u0000\fHLino\u0002\u0010\u0000\u0000mntrRGB XYZ \u0007�\u0000\u0002\u0000\t\u0000\u0006\u00001\u0000\u0000acspMSFT\u0000\u0000\u0000\u0000IEC sRGB\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000��\u0000\u0001\u0000\u0000\u0000\u0000�-HP… (truncated)
Notice the weird characters in the “text” field. It looks like the pure jpeg binary get converted to utf8 and thereby corrupting it.
So my question is that if there’s a way to preserve the binary data in the httpRequest call?
Sorry if the question is bad I’m still learning
Cheers!