首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Node.js 在 VS Code 中发送 POST 请求

Node.js 在 VS Code 中发送 POST 请求

作者头像
为为为什么
发布2022-08-10 14:04:55
发布2022-08-10 14:04:55
4K0
举报
文章被收录于专栏:又见苍岚又见苍岚

Node.js 的后端貌似更容易解析 Node 中 request 模块发送的 POST 请求,本文记录 node.js VS Code 环境配置和发送 POST 请求的方法。

背景

  • 前端小白,需求是给一个url 发送post 请求,请求中加入:
代码语言:javascript
复制
{
  "username": "your-username",
  "password": "your-password"
}

  • 对 Python 相对比较熟悉,于是先写了如下代码:
代码语言:javascript
复制
import requests
import json
url = "http://localhost:6789/"
data = {"username": "admin", "password": "xxxxxxx"}
res = requests.post(url=url, data=json.dumps(data))
print(res.text)

代码语言:txt
复制
- 返回 `400 Bad Request`在在线调试网站上尝试相同请求 
代码语言:txt
复制
- 返回 `400 Bad Request`进入目标项目的相关代码:
代码语言:javascript
复制
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (async (req, res)=>{
    const { username , password  } = req.body;
    if (!username || !password) {
        return (0,lib_response__WEBPACK_IMPORTED_MODULE_2__/* .badRequest */ .yw)(res);
    }
    const account = await (0,queries_admin_account_getAccountByUsername__WEBPACK_IMPORTED_MODULE_1__/* .getAccountByUsername */ .c)(username);
    if (account && await (0,lib_crypto__WEBPACK_IMPORTED_MODULE_0__/* .checkPassword */ .oH)(password, account.password)) {
        const { user_id , username: username1 , is_admin  } = account;
        const user = {
            user_id,
            username: username1,
            is_admin
        };
        const token = await (0,lib_crypto__WEBPACK_IMPORTED_MODULE_0__/* .createSecureToken */ .HD)(user);
        return (0,lib_response__WEBPACK_IMPORTED_MODULE_2__.ok)(res, {
            token,
            user
        });
    }
    return (0,lib_response__WEBPACK_IMPORTED_MODULE_2__/* .unauthorized */ .Hs)(res);
});

代码语言:txt
复制
- 需要的是 `req.body`
- 在 Python 发送的 `request` 里压根就没有 `body` 这东西,发送的数据在 `data` 属性里,难怪返回 400于是需要直接用 Node.js 发送 Post 请求

配置环境

安装 Node.js
配置 VS_Code
  • 安装 VS Code
  • 安装 Code Runner
  • 在项目文件夹运行
代码语言:javascript
复制
npm init --yes
npm install request --save
npm install

  • 之后可以右键运行 js 脚本
  • 也可以在代码中打断点调试

Node.js 发送 Post 请求

  • 人家 Node.js 的 request 模块啊,直接就带 body,所以就被正确解析了
  • 参考代码:
代码语言:javascript
复制
var request = require('request');
var url="http://111.33.44.55:8998/api/auth/login";
var requestData={
    "username": "admin",
    "password": "xxxxxxx"
};

httprequest(url,requestData);

function httprequest(url,data){
    console.log("hello world")
    request({
        url: url,
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        body: requestData
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body) // 请求成功的处理逻辑
        }else{
            console.log("failed")
            console.log(response.statusCode)
            console.log(error)
        }
    });
};

参考资料

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年8月9日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 配置环境
    • 安装 Node.js
    • 配置 VS_Code
  • Node.js 发送 Post 请求
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档