Post form data with axios in Node.js

You might be able to use Content-Type: ‘application/x-www-form-urlencoded’. I ran into a similar issue with https://login.microsoftonline.com where it was unable to handle incoming application/json. var axios = require(“axios”); axios({ url: ‘https://login.uber.com/oauth/v2/token’, headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’ }, data: `client_id=${encodeURIComponent(‘**’)}&client_secret=${encodeURIComponent(‘**’)}&grant_type=authorization_code&redirect_uri=${encodeURIComponent(‘http://localhost:8080/’)}&code=${encodeURIComponent(‘**’)}` }) .then(function(response) { console.log(response.data) }) .catch(function(error) { console.log(error) }) You could also use a function to handle … Read more

Axios POST request fails with error status code 500: Internal Server error

Feb 2021. Wasted 2 hours on this. Not much help on this famous library on internet. Solution: In the catch block, the error which will always be 500 internal server error so, use error.response.data instead of error. Code: try { let result = await axios.post( // any call like get “http://localhost:3001/user”, // your URL { … Read more

Get response from axios with await/async

This seems to be one of those cases where async/await doesn’t buy you much. You still need to return a result from the async function, which will return a promise to the caller. You can do that with something like: async function getData() { try { let res = await axios({ url: ‘https://jsonplaceholder.typicode.com/posts/1’, method: ‘get’, … Read more

How to post multiple Axios requests at the same time?

There are three cases via you can achieve your goal. For simultaneous requests with Axios, you can use Axios.all() axios.all([ axios.post(`/my-url`, { myVar: ‘myValue’ }), axios.post(`/my-url2`, { myVar: ‘myValue’ }) ]) .then(axios.spread((data1, data2) => { // output of req. console.log(‘data1’, data1, ‘data2’, data2) })); you can use Promise.allSettled(). The Promise.allSettled() method returns a promise that … Read more

Typescript Jest says mock or mockReturnedValue do not exist on types I wish to mock

Cast the mock method to jest.Mock, ie import axios from “axios” import Request from “./Request”; // Create an Axios mock // Don’t worry about the order, Jest will hoist this above the imports // See https://jestjs.io/docs/manual-mocks#using-with-es-module-imports jest.mock(“axios”, () => ({ create: jest.fn() })) // Customise the `create` mock method (axios.create as jest.Mock).mockReturnValue({ get: getMock })

Using JavaScript Axios/Fetch. Can you disable browser cache?

Okay so I found a solution. I had to set a timestamp on the API url to get it to make a new call. There doesn’t seem to be a way to force axios or fetch to disable cache. This is how my code now looks axios.get(`https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&timestamp=${new Date().getTime()}`) .then(response => { const { title, content, … Read more

How to retry 5xx requests using axios

axios-retry uses axios interceptor to retry HTTP requests. It intercepts requests or responses before they are handled by then or catch. Below is the working code snippet. const axios = require(‘axios’); const axiosRetry = require(‘axios-retry’); axiosRetry(axios, { retries: 3, // number of retries retryDelay: (retryCount) => { console.log(`retry attempt: ${retryCount}`); return retryCount * 2000; // … Read more

failed to load response data request content was evicted from inspector cache

The solution I found for capturing big responses is to copy the request and execute it by other means, such as PowerShell or cURL. From the Chrome or Firefox DevTools you can copy the request to a PowerShell, cURL or fetch command by right-clicking the request, selecting copy and then selecting your preferred execution method. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)