nuxt / http

Universal HTTP Module for Nuxt.js
https://http.nuxtjs.org
MIT License
229 stars 51 forks source link

How to use parameters? #173

Closed dennisfrijlink closed 3 years ago

dennisfrijlink commented 3 years ago

Maybe it's a simple question but how do I use parameters in nuxt/http? In nuxt/axios you can use parameters like:

  async asyncData({ $axios }) {
    const params = {
      consumer_key: process.env.consumer_key,
      consumer_secret: process.env.consumer_secret,
      orderby: 'date',
      per_page: 2,
      exclude: 45,
      type: 'simple'
    };

    const results = await $axios.$get('http://DOMAINNAME.com/wordpress/wp-json/wc/v3/products/?', { params })
    return { results };
  }

But this won't work using the nuxt/http module. Any idea how to pass parameters using this module?

dennisfrijlink commented 3 years ago

Never mind. I fixed it by using searchParams:

async asyncData({ $http }) {
    const params = {
      consumer_key: process.env.consumer_key,
      consumer_secret: process.env.consumer_secret,
      orderby: 'date',
      per_page: 2,
      exclude: 45,
      type: 'simple'
    };

    const res = await $http.get('https://DOMAINNAME/wordpress/wp-json/wc/v3/products/?', { searchParams: params })
    const results = await res.json()
    return { results }
}