axios

Passing headers with axios POST request

谁说胖子不能爱 提交于 2019-11-27 18:26:57
I have written an axios POST request as recommended from the npm package documentation like: var data = { 'key1': 'val1', 'key2': 'val2' } axios.post(Helper.getUserAPI(), data) .then((response) => { dispatch({type: FOUND_USER, data: response.data[0]}) }) .catch((error) => { dispatch({type: ERROR_FINDING_USER}) }) And it works, but now I have modified my backend API to accept headers. Content-Type: 'application/json' Authorization: 'JWT fefege...' Now, this request works fine on Postman, but when writing an axios call, I follow this link and can't quite get it to work. I am constantly getting

Axios interceptors and asynchronous login

天大地大妈咪最大 提交于 2019-11-27 17:27:21
I'm implementing token authentication in my web app. My access token expires every N minutes and than a refresh token is used to log in and get a new access token . I use Axios for all my API calls. I have an interceptor set up to intercept 401 responses. axios.interceptors.response.use(undefined, function (err) { if (err.status === 401 && err.config && !err.config.__isRetryRequest) { serviceRefreshLogin( getRefreshToken(), success => { setTokens(success.access_token, success.refresh_token) }, error => { console.log('Refresh login error: ', error) } ) err.config.__isRetryRequest = true err

Attach Authorization header for all axios requests

白昼怎懂夜的黑 提交于 2019-11-27 17:01:26
I have a react/redux application that fetches a token from an api server. After the user authenticates I'd like to make all axios requests have that token as an Authorization header without having to manually attach it to every request in the action. I'm fairly new to react/redux and am not sure on the best approach and am not finding any quality hits on google. Here is my redux setup: // actions.js import axios from 'axios'; export function loginUser(props) { const url = `https://api.mydomain.com/login/`; const { email, password } = props; const request = axios.post(url, { email, password });

axios: https request over proxy

守給你的承諾、 提交于 2019-11-27 14:07:46
问题 I am trying to use axios with a proxy server to make an https call: const url = "https://walmart.com/ip/50676589" var config = { proxy: { host: proxy.ip, port: proxy.port } } axios.get(url, config) .then(result => {}) .catch(error => {console.log(error)}) The proxy servers I am using are all in the United States, highly anonymous, with support for HTTP and HTTPS. I am receiving this error: { Error: write EPROTO 140736580649920:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown

Download an image using Axios and convert it to base64

妖精的绣舞 提交于 2019-11-27 11:58:08
问题 I need to download a .jpg image from a remote server and convert it into a base64 format. I'm using axios as my HTTP client. I've tried issuing a git request to the server and checking the response.data however it doesn't seem to work like that. Link to axios: https://github.com/mzabriskie/axios Link to base64 implementation: https://www.npmjs.com/package/base-64 I'm using NodeJS / React so atob/btoa doesn't work, hense the library. axios.get('http://placehold.it/32').then(response => {

How to set state of response from axios in react

穿精又带淫゛_ 提交于 2019-11-27 11:40:22
How do I set the state of a get response in axios? axios.get(response){ this.setState({events: response.data}) } You have a syntax error here. You should try this instead var self = this; axios.get('/url') .then(function (response) { console.log(response); self.setState({events: response.data}) }) .catch(function (error) { console.log(error); }); //the rest of the code var a = 'i might be executed before the server responds' There are a few things to note here: axios.get is an asynchronous function which means that the rest of the code will be executed .And when the response of the server

How can I get the status code from an http error in Axios?

不羁的心 提交于 2019-11-27 11:37:20
This may seem stupid, but I'm trying to get the error data when a request fails in Axios. axios.get('foo.com') .then((response) => {}) .catch((error) => { console.log(error) //Logs a string: Error: Request failed with status code 404 }) Instead of the string, is it possible to get an object with perhaps the status code and content? For example: Object = {status: 404, reason: 'Not found', body: '404 Not found'} What you see is the string returned by the toString method of the error object. ( error is not a string.) If a response has been received from the server, the error object will contain

Force download GET request using axios

放肆的年华 提交于 2019-11-27 10:10:36
问题 I'm using vuejs 2 + axios. I need to send a get request, pass some params to server, and get a PDF as a response. Server uses Laravel. So axios.get(`order-results/${id}/export-pdf`, { params: { ... }}) makes successful request but it does not start force downloading, even though server returns correct headers. I think this is a typical situation when you need to, say, form a PDF report and pass some filters to server. So how could it be accomplished? Update So actually I found a solution.

Vue CLI 2.0/3.0脚手架如何在本地配置mock数据

☆樱花仙子☆ 提交于 2019-11-27 10:04:40
前后端分离的开发模式已经是目前前端的主流模式,至于为什么会前后端分离的开发我们就不做过多的阐述,既然是前后端分离的模式开发肯定是离不开前端的数据模拟阶段。 我们在开发的过程中,由于后台接口的没有完成或者没有稳定之前我们都是采用模拟数据的方式去进行开发项目,这样会使我们的前后端会同时的进行,提高我们的开发效率。 因为最近自己在自学 Vue 也在自己撸一个项目,肯定会遇到使用数据的情况,所以就想着如何在前端做一些 mock 数据的处理,因为自己的项目使用的是 vue/cli 3.0 与 vue/cli 2.0 的使用有一些的不同,所以在这里记录一下。 注意:本文主要说的是如何配置本地静态文件的 mock 数据的方式 首先我们来说一说vue/cli 3.0 与 2.0 的一些不同: 3.0 移除了 static 文件目录,新增了 public 目录,这个目录下的静态资源不会经过 webpack 的处理,会被直接拷贝,所以我们能够直接访问到该目录下的资源。 3.0 移除了 config、build 等配置目录,如果需要进行相关配置我们需要在根目录下创建 vue.config.js 进行配置即可。 2.0 的文件结构 3.0 的文件结构 可以看到 3.0 版本的脚手架在项目结构上精简了很多,看上去没有那么的繁琐。接下来我就进行 mock 数据的配置,再说 3.0 之前,我们先看看 2.0

Cannot access correct this inside an axios callback [duplicate]

自作多情 提交于 2019-11-27 09:46:12
This question already has an answer here: How to access the correct `this` inside a callback? 10 answers got a little mind-fart atm. I've managed to write following code, which downloads a JSON from url and displays it on the screen: export default class Appextends React.Component { constructor(props) { super(props); this.state = { data: [], } } componentWillMount() { axios.get(//url//) .then(function (response) { localStorage.setItem('data', JSON.stringify(response.data)); //this.setState({data: response.data}); -- doesnt work }) .catch(function (error) { console.log(error); }) } render() {