首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在我的Node.js API上,我没有得到使用Axios的GET请求的响应

在我的Node.js API上,我没有得到使用Axios的GET请求的响应
EN

Stack Overflow用户
提问于 2021-11-13 23:31:56
回答 2查看 1.3K关注 0票数 0

我正在尝试用Node.js开发一个API,使用Axios,从Pipedrive获得所有的交易。我已经有了发出请求的Pipedrive,这类似于这个请求:https://mydomain.pipedrive.com/api/v1/deals?api_token=${API_TOKEN}和我也有用于身份验证的API令牌。

我已经用URL在Postman上测试了这个GET请求,它运行得很好--我正确地获得了所有事务的JSON响应--但是当我试图使用Axios发出请求时,我没有得到响应。我试过很多种方法,但都没有用。我使用Try-Catch方法创建了一个名为"getAllDealsPipedrive“的异步函数,并将axios.get(url)放在那里进行请求。我使用express的路由器函数调用路由/deals中的函数。当我在邮递员的http://localhost:8080/v1/deals上发出一个GET请求时,它会返回一个错误的请求(400)。

我对axios没有经验。如果有人能帮我处理这些请求,我会非常感激的。

我的控制器看起来像:

代码语言:javascript
复制
require('dotenv')
const Deal = require('./models/Deal')
const axios = require('axios')
const API_TOKEN = process.env.API_TOKEN
const API_URL = process.env.API_URL


class dealController {

    async getAllDealsPipedrive(req, res){

        try {
            const response = await axios.get(`${API_URL}api_token=${API_TOKEN}`, 
            {params: {data: data.body}}
            ) 
            return res.status(200).json(response)
        } 
        catch (error) {
            return res.status(400).json({"message":error})
        }
    }
}

module.exports = new Controller()

我的路由文件看起来像:

代码语言:javascript
复制
const express = require('express')
const router = express.Router()
const controller = require('../controllers/dealController')

router.get('/deals', controller.getAllDealsPipedrive)

module.exports = router

预期的JSON响应示例:

代码语言:javascript
复制
{
  "success": true,
  "data": [
    {
      "id": 1,
      "creator_user_id": {
        "id": 13540546,
        "name": "Foo Bar",
        "email": "foo.bar@test.com",
        "has_pic": 0,
        "pic_hash": null,
        "active_flag": true,
        "value": 13540546
      },
      "user_id": {
        "id": 13540546,
        "name": "Foo Bar",
        "email": "foo.bar@test.com",
        "has_pic": 0,
        "pic_hash": null,
        "active_flag": true,
        "value": 13540546
      },
      "person_id": {
        "active_flag": true,
        "name": "Foo",
        "email": [
          {
            "value": "",
            "primary": true
          }
        ],
        "phone": [
          {
            "value": "",
            "primary": true
          }
        ],
        "owner_id": 13540546,
        "value": 1
      },
      "org_id": null,
      "stage_id": 1,
      "title": "deal test",
      "value": 35,
      "currency": "BRL",
      "add_time": "2021-11-11 02:36:37",
      "update_time": "2021-11-11 02:38:47",
      "stage_change_time": null,
      "active": false,
      "deleted": false,
      "status": "won",
      "probability": null,
      "next_activity_date": null,
      "next_activity_time": null,
      "next_activity_id": null,
      "last_activity_id": null,
      "last_activity_date": null,
      "lost_reason": null,
      "visible_to": "3",
      "close_time": "2021-11-11 02:38:47",
      "pipeline_id": 1,
      "won_time": "2021-11-11 02:38:47",
      "first_won_time": "2021-11-11 02:38:47",
      "lost_time": null,
      "products_count": 0,
      "files_count": 0,
      "notes_count": 0,
      "followers_count": 1,
      "email_messages_count": 0,
      "activities_count": 0,
      "done_activities_count": 0,
      "undone_activities_count": 0,
      "participants_count": 1,
      "expected_close_date": "2021-11-25",
      "last_incoming_mail_time": null,
      "last_outgoing_mail_time": null,
      "label": null,
      "renewal_type": "one_time",
      "stage_order_nr": 0,
      "person_name": "Foo",
      "org_name": null,
      "next_activity_subject": null,
      "next_activity_type": null,
      "next_activity_duration": null,
      "next_activity_note": null,
      "group_id": null,
      "group_name": null,
      "formatted_value": "R$ 35",
      "weighted_value": 35,
      "formatted_weighted_value": "R$ 35",
      "weighted_value_currency": "BRL",
      "rotten_time": null,
      "owner_name": "Foo Bar",
      "cc_email": "test+deal1@pipedrivemail.com",
      "org_hidden": false,
      "person_hidden": false
    }
EN

回答 2

Stack Overflow用户

发布于 2021-11-14 12:22:17

您可以在调试栏(ctrl+ shift +i或F12)中检查"network“选项卡,以查看请求中正在发生什么,但是看看代码,axios在其函数中具有使用查询参数的配置,在get中如下所示:

代码语言:javascript
复制
const response = await axios.get(API_URL, {params: {api_token: API_TOKEN}})`;

但是也要看到,您尝试这样在GET请求中发送一个主体,这并不常见,即使这不起作用,axios也不需要支持使用body发送get请求,最好的解决方案是:

代码语言:javascript
复制
const response = await axios.get(API_URL, {params: {api_token: API_TOKEN}, body: data.body})`;

通过这种方式,将在URL中以查询方式发送令牌和正文,另一种方法是更改HTTP方法,将body var放在第二个参数中,如下所示:

代码语言:javascript
复制
const response = await axios.post(API_URL, data.body,{params: {api_token: API_TOKEN}})`;

但是,如果您确实需要使用GET with body,我建议使用三种方法,第一种是覆盖一些axios方法(硬方法)来使用GET with body,如果第二种方法是使用axios的拦截器,然后将主体手动放入GET请求中,然后再使用支持的其他lib。

票数 0
EN

Stack Overflow用户

发布于 2021-11-15 00:49:47

我已经解决了我的问题,毕竟使用了以下代码:

代码语言:javascript
复制
async getAllDealsPipedrive(_, res){

        try {
            const response = await axios.get(`${PIPE_URL}?api_token=${PIPE_TOKEN}`)
            return res.json(response.data)
        }
        catch (err) {
            console.log(err)
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69959110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档