Just create a config object and pass it to your Axios.get request.
const config = {
baseURL: 'www.someurl.com',
params: {
part: 'part',
maxResults: 5,
key: 'key'
}
}
axios.get('/waffles', config);
Example:
const config = {
baseURL: 'https://reqres.in',
params: {
per_page: 3
}
}
axios.get('/api/users?page=1', config).then(response => {console.log(response)}).catch(err => {console.log(err)});
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
If you would like to use axios.create you need to assign it to a variable as an axios instance object and then run your request against that instance.
var instance = axios.create({
baseURL: 'https://reqres.in',
params: {
per_page: 5
}
});
instance.get('/api/users?page=1').then(response => {console.log(response)}).catch(err => {console.log(err)});
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
So for your exact example, as I said, assign axios.create to a variable then issue your .get from that.
var instance = axios.create({
baseURL: 'http://somebigurlhere',
params: {
part: 'part',
maxResults: 5,
key: 'key'
}
});
instance.get('/search', {
params: {
q: 'word'
}
});
Bigger Edit
This edit shows how to do this in react per OPs comment in my answer. The actual sandbox demonstrating htis can be found here:
https://codesandbox.io/embed/cranky-aryabhata-2773d?fontsize=14
//axios_control.js file
import axios from "axios";
export const instance = axios.create({
baseURL: "https://reqres.in",
params: {
per_page: 5
}
});
// index.js file
Take note of the import from axios_control and the usage to log the return data right before the render.
import React from "react";
import ReactDOM from "react-dom";
import { instance } from "./axios_control";
import "./styles.css";
function App() {
instance.get("/api/users").then(response => {
console.log(response);
});
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);