nodejs使用request后端访问第三方接口

目录

一、引入依赖

1、cmd控制台下载依赖

2、对应js文件引入依赖

二、代码示例

一)、post请求

1、application/json类型

2、application/x-www-form-urlencoded类型

二)、get请求


一、引入依赖

1、cmd控制台下载依赖

npm install request

2、对应js文件引入依赖

const request = require('request')

二、代码示例

一)、post请求

1、application/json类型
    request({
        url: "https://xxxxxxxxxx.com/api/login", // api接口地址
        method: "POST", // 请求类型
        json: true, // 返回结果是否需要json格式
         // 请求头
        headers: {
            "content-type": "application/json",
        },
        // 请求体
        body: {
            fid: "1",
            fpwd: "1"
        }
    },(err, res2, body) => {
        if(err) {
            console.log("request 请求 出现错误 err : \n" , err );

        } else {
            console.log("request 请求 完成")

        }
    })
2、application/x-www-form-urlencoded类型
    request({
        url: "https://xxxxxxxxxx.com/api/login", // api接口地址
        method: "POST", // 请求类型
        json: true, // 返回结果是否需要json格式
         // 请求头
        headers: {
            "content-type": "application/x-www-form-urlencoded",
        },
        // 请求体
        form: {
            fid: "1",
            fpwd: "1"
        }
    },(err, res2, body) => {
        if(err) {
            console.log("request 请求 出现错误 err : \n" , err );

        } else {
            console.log("request 请求 完成")

        }
    })

二)、get请求

    request({
        url: "https://xxxxxxxxxx.com/api/login", // api接口地址
        method: "GET", // 请求类型
        json: true, // 返回结果是否需要json格式
         // 请求头
        headers: {
        },
        // 请求体
        qs: {
            fid: "1",
            fpwd: "1"
        }
    },(err, res2, body) => {
        if(err) {
            console.log("request 请求 出现错误 err : \n" , err );

        } else {
            console.log("request 请求 完成")

        }
    })

相关推荐

  1. nodejs使用request访问接口

    2024-07-20 09:06:04       18 阅读
  2. cool 框架 node 封装Api post请求函数

    2024-07-20 09:06:04       42 阅读
  3. nodejs 库 exiftool-vendored

    2024-07-20 09:06:04       26 阅读
  4. 调用接口-RestTemplate

    2024-07-20 09:06:04       15 阅读
  5. 调用接口-OkHttpClient

    2024-07-20 09:06:04       18 阅读
  6. 使用vue 实现跨域访问http请求

    2024-07-20 09:06:04       37 阅读
  7. webform使用ajax访问接口的两种方法

    2024-07-20 09:06:04       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 09:06:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 09:06:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 09:06:04       45 阅读
  4. Python语言-面向对象

    2024-07-20 09:06:04       55 阅读

热门阅读

  1. docker compose 部署交互模式的容器-以Ubuntu为例

    2024-07-20 09:06:04       17 阅读
  2. Spring源码系列一:入门——Hello World

    2024-07-20 09:06:04       15 阅读
  3. docker build时的网络问题

    2024-07-20 09:06:04       13 阅读
  4. Linux中Vim常用指令的笔记

    2024-07-20 09:06:04       18 阅读