$(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 Body, the request returns a 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())); 

There is an example in the documentation where the URL itself is with all the parameters except text . Okay, so I did. Still does not 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())); 

I set the body explicitly, just like in their 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 in the POST parameters should be in the body, and not in the query string?

And this seems to be not a limitation in the number of characters - I still have not exactly reached 1 million

  • @VicrotGorban and you inserted your key? - danilshik 2:58 pm
  • @danilshik, the query works in the form of get. The key is not mine, with mine the same. - Victor Gorban
  • @VictorGorban you will not believe, but the error is only one. You need to write body: 'text = hello'. Sign equal instead of a colon. - Ustyantsev Boris
  • @UstyantsevBoris, yes. For this, and need SO) - Victor Gorban
  • one
    @VictorGorban, right, because judging by the help from Yandex, it is just waiting for the usual form. That is, it was necessary to change not the content type, but the value itself which you sent. About this you came, but did not understand what and why. More details about the content types for the POST method can be found here - Grundy

1 answer 1

Thanks @UstyantsevBoris, the answer for my case was found extremely simple: You must write the body: 'text = hello'. Sign equal instead of a colon.

Update: Thanks to @Grundy, I found the complete 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 must be specified as key1=value1&key2value2 .

Pro content types for POST: link