1
Using the function fetch
I need to get the contents of an image obtained from the server response. The problem is that this image is coming with different content than it should.
Using "requests" in Python:
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xed\x00 ...
Using "fetch" in Javascript:
���� JFIF �� |Photoshop 3.0 8BIM...
I need the content obtained in fetch
is the same as that obtained in requests
Python, so that the image is saved later on the computer. Below is my code:
async function get_image_data_from(url) {
let response = await fetch(url);
return await response.text();
}
The image is given binary, you have to search as blob
response.blob();
. If you search how text will corrupt you. Take a look at this example– Augusto Vasques
@Augustovasques I’m looking for a solution that doesn’t need to install another package because I’m already using a package called
puppeteer
which has a method that executes a Javascript code in the browser and returns a value. In case I need to get the contents of the image as if I was not using Node.js but only pure JS from the browser. Is it possible to do this ? Luiz had also recommended using the methodblob
offetch
from the browser in the reply (it deleted the answer), but as I said, I need to get the string content to save the image.– JeanExtreme002
Basically the package method
puppeteer
(similar to Selenium) will return a value that will be a JS code running in the browser itself without Node. After this I saved this file using thefs
with the return (image content) of the method. If it is not possible to obtain the content without being "bugged" through thefetch
pattern, I’ll try thisnode-fetch
even.– JeanExtreme002
In your example code JS is already using
fetch
, the title of the question is about fetch. In the python example that b in front of the string means binary and if you compare the values you have in python with the example I gave in js you will see that the header is the same only it does not have the bars\
– Augusto Vasques
In case the package I used is just a browser fetch emulator for Node.
– Augusto Vasques
But when I use the method
blob
offetch
from the browser I do not receive the object like this fromnode-fetch
.– JeanExtreme002
Ai I do not know how it is behavior of synchronous fetch. But try to create an image as blob
let objectURL = URL.createObjectURL(myBlob);
 img.src = objectURL;
– Augusto Vasques
But how can I get this object buffer
Blob
to save to a file with the packagefs
?– JeanExtreme002
If it is to record a file use
arrayBuffer()
instead of blob() and save as binary.– Augusto Vasques
@Augustovasques not to get too long there, you can take me a doubt that is no longer on the subject of this question there in chat ?
– JeanExtreme002
I’m going to lunch, I’m in another time zone. But yes when I come back help you.
– Augusto Vasques
Ta then, I’ll leave the question there in the chat. Vlw and good lunch :D
– JeanExtreme002