首页 > 解决方案 > 如何像在 python 中一样发送请求并返回站点响应?

问题描述

为什么我在 js 中的代码不发送帖子数据而在 python 中有效?

在js中

const options = {
    url: "https://asd.com",
    method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Accept-Charset': 'utf-8',
            'User-Agent': 'my-reddit-client',
            'data': {"user.login":"login","user.senha":"pass"}
        }
    };

    request(options, function(err, resx, body) {        
        console.log(resx);
        res.send(body)
    });

在蟒蛇

import requests
data = {
  "user.login":"login",
  "user.senha":"pass"
}
r = requests.post('https://asd.com', params=data)
print(r.text)

我只想发送一个没有 json 格式的自定义表单,但它永远不会起作用。

标签: javascriptnode.jsexpress

解决方案


POST在 vanilla JS中提出请求:

fetch("https://asd.com", {
  method: 'POST',
  body: {"user.login":"login","user.senha":"pass"},
  headers: {
   'Accept': 'application/json',
   'Accept-Charset': 'utf-8',
   'User-Agent': 'my-reddit-client', 
  }
 }).then(res => res.json())

我建议您也阅读有关 fetch API 的信息,这样您在 JS https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API中提出任何其他类型的请求就不再困难了/Using_Fetch


推荐阅读