Node.js request return Error: connect ECONNREFUSED when set options parameter -


request works fine if send url required attributes first parameter failed each time when trying send options object parameter contains request attributes:

"use strict" var https = require('https'),     request = require('request');  var obj = {     translate: function(texttotranslate) {         var options = {             url: "https://translate.yandex.net/api/v1.5/tr.json/translate",             qs: {                 key: process.env.translation_app_token,                 lang: "en-ru",                 text: texttotranslate             }         }, translationrequest = https.request(options, function(response) {             response.on('data', function (chunk) {                 console.log(json.parse(chunk).text[0]);             });         });         console.log(options);          translationrequest.on('error', function (response) {             console.log(response);         });          translationrequest.end();     } };  obj.translate("hello"); 

i'm using qs option pass parameters tried formdata , body doesn't work well.

thank help

this works me, using request module (that you've got loaded) instead of https. , according the docs, need pass these parameters via request query params (so post form data won't work):

"use strict" var https = require('https'),     request = require('request');  var obj = {     translate: function(texttotranslate) {         var options = {             url: "https://translate.yandex.net/api/v1.5/tr.json/translate",             qs: {                 key: "<redacted>",                 lang: "en-ru",                 text: texttotranslate             }         }         request.get(options, function(e, r, body) {             console.log(body);         });     } };  obj.translate("hello"); 

tested against api valid key, , got response:

{"code":200,"lang":"en-ru","text":["привет"]} 

for it's worth, reason doesn't work you've done options https module, because syntax designed request not https. https work, need follow schema:

options = {     hostname: "translate.yandex.net",     path: "/api/v1.5/tr.json/translate?key=" + process.env.translation_app_token + "&lang=en-ru&text=" + texttotranslate } 

(docs: https://nodejs.org/api/http.html#http_http_request_options_callback)


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -