The Wayback Machine - https://web.archive.org/web/20200405183834/https://github.com/nuxt-community/axios-module/issues/97
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set query params and message body? #97

Open
Flur3x opened this issue Feb 7, 2018 · 14 comments
Open

How to set query params and message body? #97

Flur3x opened this issue Feb 7, 2018 · 14 comments
Labels

Comments

@Flur3x
Copy link

@Flur3x Flur3x commented Feb 7, 2018

Hey,

I've tried to figure out how to send payload like query params and message body, like I was used with the original Axios library. There I could just add a second argument (payload) in the method call.

Am I overseeing something? I'm quite sure I'am. Thanks for your help and for this module. :)

Best,
Daniel

This question is available on Nuxt.js community (#c85)
@pi0

This comment has been minimized.

Copy link
Member

@pi0 pi0 commented Feb 7, 2018

Hey. Exactly with this module's plugin you can use the same functions of original axios library to make requests:

const { data } = await this.$axios.post(endpoint, { data: {  }, query: {  } })

We also provide some optional helpers with exactly same params as original axios but they directly return data. So no destructuring like { data } is needed. This may make your code more readable.

const foobar = await this.$axios.$post(endpoint, { data: {  }, query: {  } })
@pi0 pi0 added question docs labels Feb 7, 2018
@Flur3x

This comment has been minimized.

Copy link
Author

@Flur3x Flur3x commented Feb 7, 2018

Thanks! I just found the actual issue. Nuxt-Axios didn't like that some endpoints already included a query param (eg. "?foo=bar") in their url. Axios itself just appends the additional query params, but Nuxt-Axios expects that the url itself doesn't contain any query.

For my case it's no big deal, but maybe you want to adjust that in a future release. :)

@pi0

This comment has been minimized.

Copy link
Member

@pi0 pi0 commented Feb 7, 2018

@Flur3x Thanks for your reports. Would you please provide some example to reproduce that error? (Also do you mean with original axios, baseURL can contain query params??)

@Flur3x

This comment has been minimized.

Copy link
Author

@Flur3x Flur3x commented Feb 7, 2018

No, I don't mean the baseURL, but the url path. As an example, let's say I have the following call:

const params = {
	articleNumber: this.articleNumber,
};

const response = await axios.$get('/api/slider?mode=bestseller', { params });

Axios sets both query params, but Nuxt-Axios only sets mode and throws away articleNumber. I have to use the regular approach to make it work:

const params = {
	articleNumber: this.articleNumber,
	mode: 'bestseller',
};

const response = await axios.$get('/api/slider', { params });
@pi0

This comment has been minimized.

Copy link
Member

@pi0 pi0 commented Feb 7, 2018

Axios sets both query params

Somehow impossible if this module has a different behavior than original axios.

axiosExtra['$' + method] = function () { return this[method].apply(this, arguments).then(res => res && res.data) }

Will keep this issue open until make sure about it. And make any fix if possible.

@Flur3x

This comment has been minimized.

Copy link
Author

@Flur3x Flur3x commented Feb 7, 2018

Okay, thanks. I also tried it without the wrapper (get() instead of $get()) with the same result.

@christopherdupont

This comment has been minimized.

Copy link

@christopherdupont christopherdupont commented Mar 15, 2018

Any news about this issue ? I have the same problem.

@gomezmark

This comment has been minimized.

Copy link

@gomezmark gomezmark commented Apr 18, 2018

Hi,
Somebody could help me

Im newbie on nuxt and im dealing with axios.
Im posting a request on axios

let data = { name :. "test" }

axios.post(url, data, {
headers : {
'Content-type' : 'application/form-url-encode'
}
}).then(...)

But on request, im passing a json stringify.
Is there a file on nuxt that should be edited?

Thanks

@SamiUrias

This comment has been minimized.

Copy link

@SamiUrias SamiUrias commented Apr 10, 2019

I am not sure if this is the correct answer but this is how I make it work for me.
I am using PHP as backend.

 let objectToSend = {
                somekey: "lorem ipsum"
            }
let propertiesResponse = await app.$axios.get('http://localhost/folder/file.php', { params: objectToSend })
            .then(response =>{ return response; }).catch(()=>{console.log('Something went wrong!')});

If I do that, using 'params' , I mean putting my object into another object with params, I can read that object using $_GET in the backend

I hope this information helps someone :D

@SacDin

This comment has been minimized.

Copy link

@SacDin SacDin commented Jun 10, 2019

Same issue. Second argument object with params not working with $axios.$get. Please mark this as a bug.

@tavaresrenan

This comment has been minimized.

Copy link

@tavaresrenan tavaresrenan commented Nov 14, 2019

So, it's not possible in Axios. Or change your API to use req.query ou change your front to use http

@nitin466

This comment has been minimized.

Copy link

@nitin466 nitin466 commented Jan 3, 2020

No, I don't mean the baseURL, but the url path. As an example, let's say I have the following call:

const params = {
	articleNumber: this.articleNumber,
};

const response = await axios.$get('/api/slider?mode=bestseller', { params });

Axios sets both query params, but Nuxt-Axios only sets mode and throws away articleNumber. I have to use the regular approach to make it work:

const params = {
	articleNumber: this.articleNumber,
	mode: 'bestseller',
};

const response = await axios.$get('/api/slider', { params });

Can we also use this to send formData (any file to upload) here along with query params. As per my limited knowledge, formData goes as separate object and below syntax is not not working for me
const response = await axios.$get('/api/slider', { {data: myFile}, { query: params} });
above line is sending query params successfully but myFile is not getting saved.

@Nurkasym

This comment has been minimized.

Copy link

@Nurkasym Nurkasym commented Mar 30, 2020

@pi0 Hi! Why POST request to api not sent data(body)?
my code:

this.$axios.$post("http://localhost:5000/api/Offers/UpdateMessage", { headers: { 'X-Auth-Token': ${this.$store.getters['auth/checkToken']}, 'Content-Type': 'application/json' }, data: JSON.stringify(body) }).then((response) => { console.log(response); })

@zgualberto

This comment has been minimized.

Copy link

@zgualberto zgualberto commented Apr 4, 2020

Hey. Exactly with this module's plugin you can use the same functions of original axios library to make requests:

const { data } = await this.$axios.post(endpoint, { data: {  }, query: {  } })

We also provide some optional helpers with exactly same params as original axios but they directly return data. So no destructuring like { data } is needed. This may make your code more readable.

const foobar = await this.$axios.$post(endpoint, { data: {  }, query: {  } })

Hi man, Can this be done with authentication? The credentials are on env file. I'm using SSR nuxt. Excuse my bad english.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
10 participants
You can’t perform that action at this time.