axios

How to return values from async functions using async-await from function?

时光毁灭记忆、已成空白 提交于 2019-11-27 04:48:41
How can I return the value from an async function? I tried to like this const axios = require('axios'); async function getData() { const data = await axios.get('https://jsonplaceholder.typicode.com/posts'); return data; } console.log(getData()); it returns me this, Promise { <pending> } You cant await something outside async scope. To get expected result you should wrap your console.log into async IIFE i.e async function getData() { return await axios.get('https://jsonplaceholder.typicode.com/posts'); } (async () => { console.log(await getData()) })() Worked sample. More information about

How to overcome the CORS issue in ReactJS

寵の児 提交于 2019-11-27 04:01:25
I am trying to make an API call through Axios in my React Application.However, Iam getting this CORS issue on my browser. I am wondering if i can resolve this issue from a client side as i donot have any access to the API internally.Attached is my code. const response = axios({ method: 'post', dataType: 'jsonp', url: 'https://awww.api.com', data: { 'appToken':'', 'request':{ 'applicationName':'ddfdf', 'userName':'jaime@dfd.com', 'password':'dfd', 'seasonIds':[1521ddfdfd5da02] } } }); return{ type:SHARE_REVIEW, payload:'response' } } Attached is my WebPack.config.js module.exports = { entry: [

Mock inner axios.create()

会有一股神秘感。 提交于 2019-11-27 02:56:03
问题 I'm using jest and axios-mock-adapter to test axios API calls in redux async action creators. I can't make them work when I'm using a axios instance that was created with axios.create() as such: import axios from 'axios'; const { REACT_APP_BASE_URL } = process.env; export const ajax = axios.create({ baseURL: REACT_APP_BASE_URL, }); which I would consume it in my async action creator like: import { ajax } from '../../api/Ajax' export function reportGet(data) { return async (dispatch, getState)

Returning data from Axios API

假如想象 提交于 2019-11-27 00:35:20
问题 I am trying to use a Node.JS application to make and receive API requests. It does a get request to another server using Axios with data it receives from an API call it receives. The second snippet is when the script returns the data from the call in. It will actually take it and write to the console, but it won't send it back in the second API. function axiosTest () { axios.get(url) .then(function (response) { console.log(response.data); // I need this data here ^^ return response.data; })

Image returned from REST API always displays broken

筅森魡賤 提交于 2019-11-26 23:44:20
问题 I am building a content management system for an art portfolio app, with React. The client will POST to the API which uses Mongoose to insert into a MongoDB. The API then queries the DB for the newly inserted image, and returns it to the client. Here's my code to connect to MongoDB using Mongoose: mongoose.connect('mongodb://localhost/test').then(() => console.log('connected to db')).catch(err => console.log(err)) mongoose.Promise = global.Promise const db = mongoose.connection db.on('error',

How do you send images to node js with Axios?

末鹿安然 提交于 2019-11-26 22:24:28
问题 Is there a way to send an array of images (or a single image) to node using axios? the axios code I'm using(I'm using react js on the front end): onFormSubmit(event){ event.preventDefault(); let payload = this.state; console.log("in onFormSubmit!!! with state: ", this.state, "and payload: ", payload); axios.post('/api/art', payload) .then(function(response){ console.log('saved successfully') }); The research I've done suggests that perhaps there isn't a supported way to send image files to

Axios Http client - How to construct Http Post url with form params

寵の児 提交于 2019-11-26 21:56:18
I am trying to create a postHTTP request with some form parameters that are to be set. I am using the axios with node server. I already have a java code implementation of constructing a url as given below: JAVA CODE: HttpPost post = new HttpPost(UriBuilder.fromUri (getProperty("authServerUrl")) .path(TOKEN_ACCESS_PATH).build(getProperty("realm"))); List<NameValuePair> formParams = new ArrayList<NameValuePair>(); formParams.add(new NameValuePair("username",getProperty ("username"))); formParams.add(new NameValuePair("password",getProperty ("password"))); formParams.add(new NameValuePair("client

Make request to SOAP endpoint using axios

流过昼夜 提交于 2019-11-26 20:43:28
问题 I need to make request to SOAP endpoint using axios in my React application. Hence I need to pass xml data in request and receive xml data in response. I have used the axios post with json data but how do I use the same for xml? PFB the code I am using for the same, but it does not work. JSON post request: var xmlData = <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> var config = { headers: {'Content-Type': 'text/xml'} };

Is it possible to use axios.all with a then() for each promise?

自古美人都是妖i 提交于 2019-11-26 20:15:13
问题 I have a React component that triggers an event to fetch data. This results in a dynamic number of stored proc calls to fetch data, and the data from each call is stored in a totally different location. Then I need to re-render once all of the data is received and available. I'm using promises with axios. Since the number of axios calls is dynamic, I'm building an array and inserting it into axios.all as follows: let promises = []; for (let i = 0; i < requests.length; i++) { promises.push

How do I set multipart in axios with react?

ぐ巨炮叔叔 提交于 2019-11-26 18:49:08
When I curl something, it works fine: curl -L -i -H 'x-device-id: abc' -F "url=http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" http://example.com/upload How do I get this to work right with axios? I'm using react if that matters: uploadURL (url) { return axios.post({ url: 'http://example.com/upload', data: { url: url }, headers: { 'x-device-id': 'stuff', 'Content-Type': 'multipart/form-data' } }) .then((response) => response.data) } This doesn't work for some reason. Here's how I do file upload in react using axios import React from 'react' import axios, { post } from 'axios'; class