简体   繁体   English

如何使用Node.JS向Ghostbin发出POST请求?

[英]How Do I Make a POST Request with Node.JS to Ghostbin?

I'm trying to send a POST request to Ghostbin via Node.JS and its request NPM module. 我正在尝试通过Node.JS及其request NPM模块将POST请求发送到Ghostbin Here's what my code looks like: 这是我的代码:

Attempt 1: 尝试1:

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

Attempt 2: 尝试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)
})

Attempt 3: 尝试3:

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

All of these attempts ended up logging: 所有这些尝试最终导致了日志记录:

<!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>

Is there something I'm missing regarding the request library, or with the documentation of the Ghostbin API ? 关于request库或Ghostbin API的文档,我缺少什么吗?

You are almost right but you need to pass data into form key(as you did in #3) and pass user-agent in header as mention in api 您几乎是正确的,但是您需要将数据传递到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)
})

this how i do it using request tested and working for me 这是我如何使用经过测试的请求并为我工作的方式

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)
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM