[Vuejs]-Capture a Response from GET and Use it in the Next Request

0👍

The Axios request config includes a headers property to specify the request’s headers. The config can be specified as the 2nd argument of axios.post() (if using the two-argument signature) or the 3rd argument (if using the three-argument signature). This example demonstrates the two-argument signature of axios.post() that sets the headers with the dataresult of a previous request:

export default {
  methods: {
    async sendRequest() {
      const userResp = await axios.get('https://reqres.in/api/users/2')
      await axios.post('https://reqres.in/api/users', {
        headers: userResp.data,
        data: {
          name: 'john doe',
          job: 'leader',
        }
      })
    },
  }
}

demo


Side note: The Access-Control-Allow-Origin is a CORS header that can only be set by the server. It has no effect when sent from the client. It’s possible you’re incorrectly assuming that header is not reaching the server because it’s not resolving a CORS issue.

Leave a comment