question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating axios instance doesn't append specified config params

See original GitHub issue

Summary

I’m using ts-jest for unit-testing, which means I don’t know if the error still occurs outside the jest environment.

Basically I’m creating a new instance using the script below. 9/10 times the params doesn’t get appended to the url.

this.api = axios.create({
	baseURL: 'https://api.themoviedb.org/3',
	params: {
		api_key: 'api key goes here',
		language: 'da-DK',
		append_to_response: 'external_ids,videos'
	}
});

Using the request interceptor I logged the config output which was this:

{ adapter: [Function: httpAdapter],
        transformRequest: { '0': [Function: transformRequest] },
        transformResponse: { '0': [Function: transformResponse] },
        timeout: 0,
        xsrfCookieName: 'XSRF-TOKEN',
        xsrfHeaderName: 'X-XSRF-TOKEN',
        maxContentLength: -1,
        validateStatus: [Function: validateStatus],
        headers: 
         { common: { Accept: 'application/json, text/plain, */*' },
           delete: {},
           get: {},
           head: {},
           post: { 'Content-Type': 'application/x-www-form-urlencoded' },
           put: { 'Content-Type': 'application/x-www-form-urlencoded' },
           patch: { 'Content-Type': 'application/x-www-form-urlencoded' } },
        method: 'get',
        baseURL: 'https://api.themoviedb.org/3',
        params: undefined,
        url: 'movie/427641' }

and as you can see, the params are undefined.

For now I’ve managed to workaround it by defining the params in the interceptor:

this.api.interceptors.request.use(config => ({
	...config,
	params: { 
		// ...
	},
}));

Context

  • axios version: latest
  • Environment: node v9.11.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

1reaction
halfzebracommented, Jul 1, 2019

Same issue, it seems like there’s a PR with a fix #2196

1reaction
cainstercommented, Jun 28, 2019

tried changing dependency to 0.18 in package.json but still not working for me ? Am I missing something?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default query params not getting passed in axios request
I solved it using 2 ways: Using default params and spreading them when using the request export const API_DEFAULT_PARAMS = { part: 'part', ......
Read more >
Request Config | Axios Docs
These are the available config options for making requests. Only the url is required. Requests will default to GET if method is not...
Read more >
Understanding Axios.create
Learn why and how to effectively use Axios.create in your next project with this in-depth tutorial and example build.
Read more >
Complete Guide to Axios HTTP Client
An Axios instance created with Axios.create() with a custom config helps us to reuse the provided configuration for all the API invocations made ......
Read more >
Sending PUT HTTP Requests with Axios
The more generic axios() call is more flexible since all options/configs are specified within the same object, making it easier to add more ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found