Nuxt.JS: How to get post parameter data in a vue file

675 views Asked by At

I am integrating paytm payment gateway in my nuxt js project. Buy I am unable to get post data with which paytm is redirecting to my callback url.

1

There are 1 answers

1
Sergio Cerrutti On

Since asyncData gives you access to req and res (see https://nuxtjs.org/guide/async-data/#use-reqres-objects), you can parse the body to get POST parameters. I found this solution out there, so I didn't try it out. In fact, it should be better to use a Middleware to parse this and load the middleware in that page.

<script>
export default {
  asyncData ({ req, res }) {
    if (process.server) {
      const qs = require('querystring')
      let body = ''
      let temp = ''
      let paymentData = ''

      while(temp = req.read()) {
        body += temp
      }

      paymentData = qs.parse(body)

      return { data: paymentData }
    }
  },

  data () {
    return {
      paymentData: '',
    }
  },

  mounted () {
    console.log(this.paymentData)
  }
</script>