我如何修复在 nodejs 中发出 POST 请求时遇到的错误
How do I fix this error that I'm getting while making a POST request in nodejs
因此,我使用 Reddit API 将以下代码写入 post Reddit 上的图像 API。
const request = require('request')
let access_token = "redacted"
let url = 'https://oauth.reddit.com/api/submit'
let headers = {
'Content-type': 'application/x-www-form-urlencoded',
"Authorization": "bearer " + access_token,
"User-Agent": "manage your reddit easily in the maxi time created in some idea of mine by u/_jaypatel"
}
let data = {
'sr':'testingground4bots',
'api_type': "json",
'title': 'Testing post creation',
'spoiler': false,
'nsfw' : false,
'resubmit': false,
'sendreplies': false,
'text':"This is raw api test",
'kind':'image',
'url':'https://i.imgur.com/fLZ35cI.jpg',
"extension": "json"
}
var options = {
url, headers, data
}
request.post(options,function (error, response, body) {
if (error) {
console.log(error);
}
else{
console.log(body);
}
}
);
我一次又一次地收到这个错误。
{"jquery": [[0, 1, "call", ["body"]], [1, 2, "attr", "find"], [2, 3, "call", [".status"]], [3, 4, "attr", "hide"], [4, 5, "call", []], [5, 6, "attr", "html"], [6, 7, "call", [""]], [7, 8, "attr", "end"], [8, 9, "call", []], [1, 10, "attr", "find"], [10, 11, "call", [".error.BAD_SR_NAME.field-sr"]], [11, 12, "attr", "show"], [12, 13, "call", []], [13, 14, "attr", "text"], [14, 15, "call", ["This community name isn't recognizable. Check the spelling and try again."]], [15, 16, "attr", "end"], [16, 17, "call", []]], "success": false}
我首先写了一个 Python 脚本如下,它成功了,我想用 Node JS 写一个但是我做不到。这是 python 代码
import requests
access_token = "redacted"
headers = {
"Authorization": "bearer " + access_token,
"User-Agent": "manage your reddit easily in the maxi time created in some idea of mine by u/_jaypatel"
}
data = {
'sr':'testingground4bots',
'api_type': "json",
'title': 'Testing post creation',
'spoiler': False,
'nsfw' : False,
'resubmit': False,
'sendreplies': False,
'text':"This is raw api test",
'kind':'image',
'url':'https://i.imgur.com/fLZ35cI.jpg',
"extension": "json",
"uh":"74641dhxxdd1d8da2f8d06d267dec57369ffcb6098cc7cd2f1"
}
r = requests.post("https://oauth.reddit.com/api/submit", headers=headers, data=data)
content = r.json()
print(content)
如何修复我的 Node JS 脚本以使其正常工作?任何帮助将不胜感激。
我自己找到了答案。我好笨啊啊啊啊
在这里,改变这个
var options = {
url, headers, data
}
至此
var options = {
url, headers, form:data
}
因此,我使用 Reddit API 将以下代码写入 post Reddit 上的图像 API。
const request = require('request')
let access_token = "redacted"
let url = 'https://oauth.reddit.com/api/submit'
let headers = {
'Content-type': 'application/x-www-form-urlencoded',
"Authorization": "bearer " + access_token,
"User-Agent": "manage your reddit easily in the maxi time created in some idea of mine by u/_jaypatel"
}
let data = {
'sr':'testingground4bots',
'api_type': "json",
'title': 'Testing post creation',
'spoiler': false,
'nsfw' : false,
'resubmit': false,
'sendreplies': false,
'text':"This is raw api test",
'kind':'image',
'url':'https://i.imgur.com/fLZ35cI.jpg',
"extension": "json"
}
var options = {
url, headers, data
}
request.post(options,function (error, response, body) {
if (error) {
console.log(error);
}
else{
console.log(body);
}
}
);
我一次又一次地收到这个错误。
{"jquery": [[0, 1, "call", ["body"]], [1, 2, "attr", "find"], [2, 3, "call", [".status"]], [3, 4, "attr", "hide"], [4, 5, "call", []], [5, 6, "attr", "html"], [6, 7, "call", [""]], [7, 8, "attr", "end"], [8, 9, "call", []], [1, 10, "attr", "find"], [10, 11, "call", [".error.BAD_SR_NAME.field-sr"]], [11, 12, "attr", "show"], [12, 13, "call", []], [13, 14, "attr", "text"], [14, 15, "call", ["This community name isn't recognizable. Check the spelling and try again."]], [15, 16, "attr", "end"], [16, 17, "call", []]], "success": false}
我首先写了一个 Python 脚本如下,它成功了,我想用 Node JS 写一个但是我做不到。这是 python 代码
import requests
access_token = "redacted"
headers = {
"Authorization": "bearer " + access_token,
"User-Agent": "manage your reddit easily in the maxi time created in some idea of mine by u/_jaypatel"
}
data = {
'sr':'testingground4bots',
'api_type': "json",
'title': 'Testing post creation',
'spoiler': False,
'nsfw' : False,
'resubmit': False,
'sendreplies': False,
'text':"This is raw api test",
'kind':'image',
'url':'https://i.imgur.com/fLZ35cI.jpg',
"extension": "json",
"uh":"74641dhxxdd1d8da2f8d06d267dec57369ffcb6098cc7cd2f1"
}
r = requests.post("https://oauth.reddit.com/api/submit", headers=headers, data=data)
content = r.json()
print(content)
如何修复我的 Node JS 脚本以使其正常工作?任何帮助将不胜感激。
我自己找到了答案。我好笨啊啊啊啊
在这里,改变这个
var options = {
url, headers, data
}
至此
var options = {
url, headers, form:data
}