3

I'm trying to download part of text file by fetch with range header property. But I get corrupted character from fetch.

Here part of my text file:

Dalgalan sen de şafaklar gibi ey şanlı hilâl,
Olsun artık dökülen kanlarımın hepsi helâl;
Ebediyen sana yok, ırkıma yok izmihlâl:
Hakkıdır, hür yaşamış bayrağımın hürriyet;
Hakkıdır, Hakk’a tapan milletimin istiklâl!

and my javascript code is:

fetch("https://website.com/images/text.txt", {
        headers: {
          'content-type': 'text/plain;charset=UTF-8',
            'range': 'bytes=17-202'
        },
    })
    .then(response => {
        if (response.ok) {
            return response.text();
        }
    })
    .then(response => {
        console.log(response);
    });

The result is:

�afaklar gibi ey şanlı hilâl;
Olsun artık dökülen kanlarımın hepsi helâl.
Ebediyen sana yok, ırkıma yok izmihlâl:
Hakkıdır, hür yaşamış bayrağımın hürriyet;
Hakkıd

As you can see the first character is unknown symbol, question mark, but it has to be ş.

I think non english charecters have 2 bytes so which are splited into. How can I solve this, thank you :)

dnaz
  • 276
  • 5
  • 14

1 Answers1

1

The Content-Type header doesn't affect the response from the fetch, it is used to tell the browser and the URL what you're sending. So it is meant to be used with POST requests. (But there is no harm using it here to).

The Accept header is the exact opposite to the Content-Type, the Accept header should always be used when making a GET request, so you always know what kind of data to expect.

Although they have different use, their values are the same. So you should set your Accept header to the text/plain;charset=UTF-8 value along with the other headers of your choice. Like so

fetch("https://website.com/images/text.txt").then(response => response.text()).then(...)
a.mola
  • 3,883
  • 7
  • 23