axios

Mock axios with axios-mock-adapter get undefined resp

孤人 提交于 2019-12-05 10:47:59
I created an axios instance ... // api/index.js const api = axios.create({ baseURL: '/api/', timeout: 2500, headers: { Accept: 'application/json' }, }); export default api; And severals modules use it .. // api/versions.js import api from './api'; export function getVersions() { return api.get('/versions'); } I try to test like .. // Test import { getVersions } from './api/versions'; const versions= [{ id: 1, desc: 'v1' }, { id: 2, desc: 'v2' }]; mockAdapter.onGet('/versions').reply(200, versions); getVersions.then((resp) => { // resp is UNDEFINED? expect(resp.data).toEqual(versions); done();

Cannot Basic Auth from React App with Axios or SuperAgent

混江龙づ霸主 提交于 2019-12-05 10:45:29
I try to make a GET request with axios and I always get 401. This happens only when I send the request from my react app. axios.get('http://localhost:8080/vehicles', { withCredentials: true, auth: { username: 'admin', password: 'admin' }, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, }) Postman GET request with the same credentials and Basic Auth set, works. If I simply type in my browser the url, a login box appears and with the same credentials, it works, I get what I want. I tried with SuperAgent too: Request.get("http://localhost:8080/vehicles")

How to pass form values as FormData in reactjs on submit function

时光怂恿深爱的人放手 提交于 2019-12-05 09:43:09
I have a dynamic form generated using json data and I need to pass the form input values on submit. I'm planning to send the values as formdata. I have created submit function but i dont know how to append the values in formdata and need to pass through post method using Axios. Im new to react can anyone tell me how to do this. Below is my code. var DATA = { "indexList": [{ "Label": "Name", "Type": "text", "Regex": "", "Default_Val": "", "Values": { "Key": "", "Value": "" }, "Validtion Msg": "", "Script": "", "Mandatory": "Y", "maxLength":"16", "minLength":"7", "format":"Alphanumeric",

post request using axios on Laravel 5.5

大兔子大兔子 提交于 2019-12-05 08:51:42
i'm trying to make some requests using axios and the last Laravel version 5.5 after configure the X-CSRF fields and all my code is simple : axios.post('/post-contact',{name:'Kamal Abounaim'}) .then((response)=>{ console.log(response) }).catch((error)=>{ console.log(error.response.data) }) but i get this error : 419 (unknown status) what the problem supposed to be Thanks for answering This is happening because of the csrf-token. Just add meta tag with the csrf-token in the <head> and add that token to axios header like so. // in the <head> <meta name="csrf-token" content="{{ csrf_token() }}">

Sending post request multipart form data. Error from some microsoft service “Line length limit 100 exceeded”

依然范特西╮ 提交于 2019-12-05 08:47:20
This data is sent from Postman and it works: This is a postman request which passes with a 200 status: POST /api/upload HTTP/1.1 Host: api.test.contoso.se Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW Authorization: Basic 123 User-Agent: PostmanRuntime/7.13.0 Accept: */* Cache-Control: no-cache Postman-Token: 089af753-fa12-46c4-326f-dfc39c36faab,c5977145-ece3-4b53-93ff-057788eb0dcf Host: api.test.contoso.se accept-encoding: gzip, deflate content-length: 18354 Connection: keep-alive cache-control: no-cache Content-Disposition: form-data; name="Lang" SV -----

componentDidMount: Can't call setState (or forceUpdate) on an unmounted component

北城以北 提交于 2019-12-05 06:59:47
I am fetching data in componentDidMount and updating the state and the famous warning is appearing: Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method. My code is as follow: componentDidMount() { let self = this; let apiBaseUrl = Config.serverUrl; axios.get( apiBaseUrl + '/dataToBeFetched/' ) .then( function(response) { self.setState( { data: response.data } );; } ); } What is causing this warning and what is the

Error: getaddrinfo ENOTFOUND

血红的双手。 提交于 2019-12-05 04:26:48
I have a simple Node.js bot that makes an HTTP request each second to a rest API. If the returned data is right then I construct an URL where I HTTP POST. Everything works alright but after ~4-5hrs of running I got this error 0|server | error: Error: getaddrinfo ENOTFOUND www.rest-api.com www.rest-api.com:443 0|server | at errnoException (dns.js:28:10) 0|server | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:73:26) Can someone explain to me why this has happened? After I restart my server everything got working. I'm using axios to make the http requests. William I met the same issue

How to get response times from Axios

纵然是瞬间 提交于 2019-12-05 04:13:27
Can anyone suggest any ways to get response times from Axios? I've found axios-timing but I don't really like it (controversial, I know). I'm just wondering if anyone else has found some good ways to log response times. Sagar M You can use the interceptor concept of axios. Request intercepor will set startTime axios.interceptors.request.use(function (config) { config.metadata = { startTime: new Date()} return config; }, function (error) { return Promise.reject(error); }); Response interceptor will set endTime & calculate the duration axios.interceptors.response.use(function (response) {

Axios: Upload progress for multiple file uploads

喜欢而已 提交于 2019-12-05 02:36:58
问题 Following https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html I've set up a file upload with progress bar. However, I have <input type="file" multiple> , so the upload is inside a loop like this: for (var i=0; i<files.length; i++) { var config = { onUploadProgress: function(progressEvent) { var what = Math.round( (progressEvent.loaded * 100) / progressEvent.total ); } }; axios.post(url, data, config) .then(function (response) { }); } The question is: How can I assign

How to use async / await in get request using vue + axios?

允我心安 提交于 2019-12-04 23:56:25
问题 I have the following code and would like to know how I can implement a try / catch with async / await executing the same function: import Vue from 'vue' import axios from 'axios' new Vue({ el: '#app', data: { skills: [], }, mounted() { axios .get('http://localhost:8080/wp-json/api/v1/skills') .then(response => { this.skills = response }).catch(err => (console.log(err))) } }) Thank you! 回答1: see code below: var app = new Vue({ el: '#app', async mounted() { try{ let response = await axios.get(