Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios get params does not inherit the params in create

Playing around with youtube api and reactjs

I am calling youtube api. Recently noticed there is create in axios so I wanted to use it but somehow params kept on overwritten

What am I doing wrong here?

I have a file named youtube.api

import axios from 'axios';

export default axios.create({
    baseURL: 'https://www.googleapis.com/youtube/v3',
    params: {
        part: 'snippet',
        key: 'blahkey',
    }
});

then inside my react handleOnSubmit import youtube from '../apis/youtube';

handleOnSubmit = async (e) => {
    e.preventDefault();
    console.log(this.state.query);
    const response = await youtube.get('/search', {
        params: { q: this.state.query }
    });

    console.log(response, 'response');

};

console.log(response, 'response');

I get an error of https://www.googleapis.com/youtube/v3/search?q=book 400

the params of part and key are missing from the url though.

Can someone please give me a hand?

Thanks in advance

like image 370
Dora Avatar asked Jun 05 '19 00:06

Dora


People also ask

How to pass params in axios get?

In this section, we will learn how to make Axios GET requests with query parameters. First, add the following code to the index. js file: // Axios GET Query Parameters const url = require("url"); const queryParams = { limit: 1, sort: "desc", }; const params = new url.

How do you pass header and params in Axios request?

How do you pass headers and params in Axios? When you are using the Axios library and to pass custom headers, you need to construct headers as an object with the key name 'headers'. The 'headers' key should contain an object, here it is Content-Type and Authorization .

How do I get Axios data?

A GET request can be made with Axios to “get” data from a server. The HTTP get request is performed by calling axios. get() . The get() method requires two parameters to be supplied to it.

How do I create an Axios instance?

First, we require Axios as we have already installed it in the previous step. Then, we use axios. create to create a new instance of Axios with a custom config that has a base URL of https://api.GitHub.com/ and a timeout of 1s. The config also has an Accept header with value application/vnd.


1 Answers

I found out that this might be due to new version issue. I am using "axios": "^0.19.0" which is causing this issue.

I downgrade it to "axios": "^0.18.0" and "axios": "^0.18.1", both worked fine

P.S. I went through their issue tickets on github and this was posted 6 days ago https://github.com/axios/axios/issues/2190

like image 55
Dora Avatar answered Oct 17 '22 07:10

Dora