How do I make a POST request to the Yandex Translate API?

$(document).ready(function () {
    let url = "https://translate.yandex.net/api/v1.5/tr.json/detect?key=trnsl.1.1.20150402T131655Z.e753695703b45806.bda2fd6beb5bd56a62f0034352aaebbdba3f0952&text=hey";
    let res = fetch(url, {
            method: 'POST',
            headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded'
            })

        });
});

This is my request. Looks like GET, right? The problem is that if I try to push at least one parameter into the Body, the query returns Bad Request.

let url = "https://translate.yandex.net/api/v1.5/tr.json/translate";
let res = fetch(url, {
        method: 'POST',
        body:JSON.stringify({key:"trnsl.1.1.20150402T131655Z.e753695703b45806.bda2fd6beb5bd56a62f0034352aaebbdba3f0952", lang:"en-ru", text:"hello"}),
        headers: new Headers({
            'Content-Type': 'application/x-www-form-urlencoded'
        })

    }).then(ok=>console.log(ok.json()));

In the documentation there is an example where the URL itself with all parameters except text. Okay, I did it anyway. It still doesn't work:

let url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150402T131655Z.e753695703b45806.bda2fd6beb5bd56a62f0034352aaebbdba3f0952&lang=en-ru";
let res = fetch(url, {
        method: 'POST',
        body:JSON.stringify({text:"hello"}),
        headers: new Headers({
            'Content-Type': 'application/x-www-form-urlencoded'
        })

    }).then(ok=>console.log(ok.json()));

Set the body explicitly, just like they have in the documentation:

let url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150402T131655Z.e753695703b45806.bda2fd6beb5bd56a62f0034352aaebbdba3f0952&lang=en-ru";
let res = fetch(url, {
        method: 'POST',
        body:'text:hello',
        headers: new Headers({
            'Content-Type': 'application/x-www-form-urlencoded'
        })

    }).then(ok=>console.log(ok.json()));

You can check in the console. What am I doing wrong? I understand that, that in POST, the parameters should be in the body, not in the query string?

And it doesn't seem to be a limit in the number of characters - I haven't exactly reached 1 million yet

Author: Victor Gorban, 2019-04-19

1 answers

Thanks to @Ustyantsevboris, the answer for my case was found very simply: It is necessary to write body:'text=hello'. An equal sign instead of a colon.

Update: Thanks to @Grundy, I found the full answer.

Here is a working example:

let url = "https://translate.yandex.net/api/v1.5/tr.json/translate";
let res = fetch(url, {
        method: 'POST',
        body:'key=trnsl.1.1.20190419T144929Z.9a7ce55bcbc0ab3a.7b061e1931fe57955befd67ab7151772bed63f0f&lang=en-ru&text=hello',
        headers: new Headers({
            'Content-Type': 'application/x-www-form-urlencoded'
        })

    }).then(ok=>console.log(ok.json()));

For Content-Type: application/x-www-form-urlencoded, the body should be set as key1=value1&key2value2.

About POST content types: link

 0
Author: Victor Gorban, 2019-04-21 08:31:45