我正在尝试通过Node.JS及其request NPM模块向Ghostbin发送POST请求。这是我的代码:

尝试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库或the documentation of the Ghostbin API我缺少什么吗?

最佳答案

您几乎是正确的,但是您需要将数据传递到form密钥中(就像您在#3中所做的那样),并按照api中的说明在标头中传递user-agent

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

10-04 15:33