0

I'm trying to develop an API with Node.js using Axios, to get all deals from Pipedrive. I already have the Pipedrive URL to make the requests, which is similar to this one: https://mydomain.pipedrive.com/api/v1/deals?api_token=${API_TOKEN} and I have also the API token for authentication.

I've tested this GET request on Postman, with the URL, and it works just fine - I get the JSON response with all the deals correctly - but when I'm trying to make the request using the Axios I get no response. I've tried to do it in so many ways, but none of them really worked. I've created an async function called "getAllDealsPipedrive" using a Try-Catch method, and put the axios.get(url) there to make the request. I'm calling the function in my route /deals using the Router function from express. When I make a GET request on http://localhost:8080/v1/deals on the Postman it returns me a bad request (400).

I have no experience with axios. I would really appreciate if someone could help me to get these requests work.

My controller looks like:

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()

My routes file looks like:

const express = require('express')
const router = express.Router()
const controller = require('../controllers/dealController')

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

module.exports = router

An example of the expected JSON response:

{
  "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
    }
  • 1
    What's API_URL set to? Does it end with "?" ? – Robert Kawecki Nov 13 '21 at 23:37
  • The URL seems like this one: It has the domain name at the beginning and its finish with the api token for authentication. I've tested and it works fine. I've also used the full URL address directly in my code to test the requests, but as I said, it doesn't worked. My API is running normally, I have a status function to validate it. I belive that the error is with my get require, with axios. – Marco Targino Nov 13 '21 at 23:55
  • 1
    Try console.log'ging the final URL that you're requesting. Does it look OK? – Robert Kawecki Nov 14 '21 at 01:11
  • I've tested the code on [this answer](https://stackoverflow.com/a/69008504/3225373) locally and it works fine. It's very similar to yours, so you should be able to make it work from there. – herbae Nov 14 '21 at 03:53

2 Answers2

0

you can check "network" tab in debug bar(ctrl + shift + i or F12), to see wha is happening in your request, but looking you code, the axios lib havea config to use query params in their functions, in get looks like:

const response = await axios.get(API_URL, {params: {api_token: API_TOKEN}})`;

But see too, that you trying so send a body in GET request, this not usual, even that doesn't work, axios does not have to support to send get reques with body, to the best solution is:

const response = await axios.get(API_URL, {params: {api_token: API_TOKEN}, body: data.body})`;

In this way, token and body will be sent in query in URL, other way is change HTTP method to POST and put the body var in second argument, like this:

const response = await axios.post(API_URL, data.body,{params: {api_token: API_TOKEN}})`;

But if you really need to use GET with body, I recommend three approaches, first is overwrite some axios methods(the hardway) to use GET with body, if second is use interceptor of axios, and manually put the body in GET request, and the thrird is use other lib that have support

Phil
  • 157,677
  • 23
  • 242
  • 245
Caio Filus
  • 705
  • 3
  • 17
  • Thanks! I've tried the first one using the "params" and it actually doesn't worked, but it worked without the "params", like this one: `const response = await axios.get('${API_URL}${API_TOKEN}')`. I'm using an .env file to store my variables with sensitive information. The URL already contains in its body the token for athentication, so I can use it directly in `axios.get(url)` with a response: `res.json(response.data)` – Marco Targino Nov 14 '21 at 16:08
  • I'd upvote this except for all that stuff about `body`. GET requests do not have a request body and `body` isn't even an Axios config option – Phil Nov 15 '21 at 23:28
0

I've solved my problem, after all using this code:

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)
        }
    }