Как сделать POST-запрос с Node.JS на Ghostbin? - PullRequest
0 голосов
/ 25 мая 2018

Я пытаюсь отправить запрос POST на Ghostbin через Node.JS и его request модуль NPM.Вот как выглядит мой код:

Попытка 1:

reqest.post({
   url: "https://ghostbin.com/paste/new",
   text: "test post"
}, function (err, res, body) {
   console.log(res)
})

Попытка 2:

reqest.post({
   url: "https://ghostbin.com/paste/new",
   text: "test post",
   headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Content-Length": 9
   }
}, function (err, res, body) {
   console.log(res)
})

Попытка 3:

reqest.post("https://ghostbin.com/paste/new", {form: {text: "test post"}}, function (err, res, body) {
   console.log(res)
})

Всеиз этих попыток закончилось ведение журнала:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>406 Not Acceptable</title>\n</head><body>\n<h1>Not Acceptable</h1>\n<p>An appropriate representation of the requested resource /paste/new could not be found on this server.</p>\n<hr>\n<address>Apache/2.4.18 (Ubuntu) Server at ghostbin.com Port 443</address>\n</body></html>

Что-то мне не хватает в библиотеке request или в документации API Ghostbin ?

Ответы [ 2 ]

0 голосов
/ 25 мая 2018

вот как я это делаю, используя проверенный запрос и работая для меня

var request = require('request'); 
var url= 'your link here';
var headers = { // add the browser sent header request data
'User-Agent':       "Mozilla/5.0 (Windows NT 6.3; rv:48.0) Gecko/20100101      Firefox/48.0",
'Content-Type':     'application/x-www-form-urlencoded'    
}
// Configure the request
var options = {
url: url,
method: 'POST',
headers: headers,
form: {'user': 'value', 'pass': 'value'}
}
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}); 
0 голосов
/ 25 мая 2018

Вы почти правы, но вам нужно передать данные в ключ form (как вы это делали в # 3) и передать user-agent в заголовке, как упомянуто в API

reqest.post({
   url: "https://ghostbin.com/paste/new",
   form: {
     text: "test post"
   },
   headers: {
      'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
   }
}, function (err, res, body) {
   console.log(res)
})
...