x-magix
x-magix

Reputation: 2852

Sending request from server to same server with axios

I need to send request from server to same server with axios as PUT method exmp.

await axios({
        url: `http://localhost:4000${url}`,
        method: requestType,
        headers: { ...req.headers, 'Content-Type': 'multipart/form-data' },
        data,
 }).catch((err) => console.log('error :>> ', err))

the problem is that I am not getting any error and request is not sent. After while I receive Timeout error

Upvotes: 0

Views: 1583

Answers (1)

Noslac
Noslac

Reputation: 872

You should check and ensure $url, requestType and ...req.headers are correct and that you can curl that endpoint. And why not ask your question with their correct values instead of using variables whose values we don't know. In any case, here's something working in express

const axios = require('axios')
const express = require('express')
const app = express()
const port = 4000

app.get('/', (req, res) => {
 res.send('Hello World!')
})

const url = `http://localhost:${port}`;

app.listen(port, () => {
 console.log(`Example app listening at ${url}`)
})

axios({
 url: url,
 method: 'get', // tried with post and put too
})
.then(res => {
 console.log(res.data)
})
.catch((err) => console.log('error :>> ', err))

Upvotes: 1

Related Questions