Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

node.js - Cant`t insert Cyrillic symbols in POST fetch URL - NodeJS

Working on Telegram bot. A user sends requests from the bot to an external website API to get information. The very usual stuff.

I'm trying to make a POST request from NodeJS (Express) backend which contains cyrillic symbols https://somewebsite.ru/api/v1/order.json?orderId=**МУЗ**008134

it says: TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters

Then I was trying to use ${encodeURIComponent(orderNumber)} instead of ${orderNumber} Error checnged to FetchError: invalid json response body at https://somewebsite.ru/api/v1/order.json?orderId=%D0%9C%D0%A3%D0%97008058 reason: Unexpected end of JSON input

When I use Postman there is no problem. I just put the whole URL https://somewebsite.ru/api/v1/order.json?orderId=МУЗ008134 and it works fine.

the server has utf8 encoding. I'm using WebStorm and VS Code - both are set with UTF8

Here is the code:

oneOrder: async (orderNumber) => {
    try {
        let url = `https://somewebsite.ru/api/v1/order.json?orderId=${orderNumber}`
        return fetch(url, {
            method: 'post',
            headers: { 'Content-Type': 'text/plain; charset=utf-8' }
        })
        .then(res => res.json())
        .then(answer => {
            if (answer.error) {
                return answer.message
            } else if (answer.orderId) {
                return `Номер заказа: ${answer['orderId']}
Создан ${answer['createdAt']}
Общая стоимость товаров в заказе: ${answer['totalCost']}
Статус оплаты: ${answer['status']['payment']}
Статус доставки: ${answer['status']['delivey']}`
            }
            return 'Нет информации по заказу'
        })

    } catch (e) {
        console.log('ERROR with oneOrder function:', e)
    }
},

...and, by the way, I have no idea why the "МУЗ008134" is not showed as a part of URL, but as a ppendix to the URL.

Thanks a lot and sorry if it seems to be too obvious.

question from:https://stackoverflow.com/questions/65936251/cantt-insert-cyrillic-symbols-in-post-fetch-url-nodejs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...