NodeJS, Axios – post file from local server to another server

The 2 oldest answers did not work for me. This, however, did the trick: const FormData = require(‘form-data’); // npm install –save form-data const form = new FormData(); form.append(‘file’, fs.createReadStream(file.path)); const request_config = { headers: { ‘Authorization’: `Bearer ${access_token}`, …form.getHeaders() } }; return axios.post(url, form, request_config); form.getHeaders() returns an Object with the content-type as well … Read more

Passing headers with axios POST request

When using Axios, in order to pass custom headers, supply an object containing the headers as the last argument Modify your Axios request like: const headers = { ‘Content-Type’: ‘application/json’, ‘Authorization’: ‘JWT fefege…’ } axios.post(Helper.getUserAPI(), data, { headers: headers }) .then((response) => { dispatch({ type: FOUND_USER, data: response.data[0] }) }) .catch((error) => { dispatch({ type: … Read more

‘Access-Control-Allow-Origin’ issue when API call made from React (Isomorphic app)

CORS is a browser feature. Servers need to opt into CORS to allow browsers to bypass same-origin policy. Your server would not have that same restriction and be able to make requests to any server with a public API. https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Create an endpoint on your server with CORS enabled that can act as a proxy … Read more

how to cancel/abort ajax request in axios

Axios does not support canceling requests at the moment. Please see this issue for details. UPDATE: Cancellation support was added in axios v0.15. EDIT: The axios cancel token API is based on the withdrawn cancelable promises proposal. UPDATE 2022: Starting from v0.22.0 Axios supports AbortController to cancel requests in fetch API way: Example: const controller … Read more

sending file and json in POST multipart/form-data request with axios

To set a content-type you need to pass a file-like object. You can create one using a Blob. const obj = { hello: “world” }; const json = JSON.stringify(obj); const blob = new Blob([json], { type: ‘application/json’ }); const data = new FormData(); data.append(“document”, blob); axios({ method: ‘post’, url: ‘/sample’, data: data, })

Sending the bearer token with axios

const config = { headers: { Authorization: `Bearer ${token}` } }; const bodyParameters = { key: “value” }; Axios.post( ‘http://localhost:8000/api/v1/get_token_payloads’, bodyParameters, config ).then(console.log).catch(console.log); The first parameter is the URL. The second is the JSON body that will be sent along your request. The third parameter are the headers (among other things). Which is JSON as … Read more

Handling async request with React, Redux and Axios?

Your redux action creators must be plain, object and should dispatch and action with a mandatory key type. However using custom middlewares like redux-thunk you could call axios request within your action creators as without custom middlewares your action creators need to return plain object Your action creator will look like export function create (values) … Read more