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 :)

1

1 Answer 1

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(...)
Sign up to request clarification or add additional context in comments.

5 Comments

Okay... have you tried fetching the file without any options? Like in my edited answer
Without any of them?
Yes, when I run fetch with without headers, I download all file with no errors.
Yes I got the text, original text.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.