【问题标题】:How do I fix this error that I'm getting while making a POST request in nodejs如何解决在 nodejs 中发出 POST 请求时遇到的此错误
【发布时间】:2021-07-24 22:06:52
【问题描述】:

所以,我编写了以下代码,使用 Reddit API 在 Reddit 上发布图片。

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 脚本以使其正常工作?任何帮助将不胜感激。

【问题讨论】:

  • 显而易见的问题:您的 Python 脚本有一个 uh 值,而您的 Node 脚本没有...为什么?
  • uh 不是强制性的,即使我包含 uh,它也会给我同样的错误

标签: python node.js reddit


【解决方案1】:

我自己找到了答案。我好笨啊啊啊啊啊

在这里,改变这个

var options = {
  url, headers, data
}

到这里

var options = {
  url, headers, form:data
}

【讨论】:

    猜你喜欢
    • 2021-01-04
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2018-04-27
    • 2018-11-02
    • 1970-01-01
    • 2019-06-14
    相关资源
    最近更新 更多