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": "[email protected]",
"has_pic": 0,
"pic_hash": null,
"active_flag": true,
"value": 13540546
},
"user_id": {
"id": 13540546,
"name": "Foo Bar",
"email": "[email protected]",
"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 [email protected]",
"org_hidden": false,
"person_hidden": false
}
CodePudding user response:
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
CodePudding user response:
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)
}
}