Answers for "axios.post data"

11

axios post with header

// Send a POST request
axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  },
  headers: {'Authorization': 'Bearer ...'}
});
Posted by: Guest on April-13-2020
44

axios post request

// axios POST request
const options = {
  url: 'http://localhost:3000/api/home',
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json;charset=UTF-8'
  },
  data: {
    name: 'David',
    age: 45
  }
};

axios(options)
  .then(response => {
    console.log(response.status);
  });
Posted by: Guest on October-08-2021
28

axios get

const axios = require('axios');

// Make a request for a user with a given ID
axios.get('/user?ID=12345')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

// Optionally the request above could also be done as
axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {
    // always executed
  });  

// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
  try {
    const response = await axios.get('/user?ID=12345');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}
Posted by: Guest on July-18-2020
6

axios api post request

import qs from 'qs';
const data = { 'bar': 123 };
const options = {
  method: 'POST',
  headers: { 'content-type': 'application/x-www-form-urlencoded' },
  data: qs.stringify(data),
  url,
};
axios(options);
Posted by: Guest on August-08-2020
0

axios.post

<script>
	export default {
      data(){
          return {
              urlBase: 'http://localhost:8000/api/v1/marca',
              nomeMarca: '',
              arquivoImagem: []
          }
      },
      methods: {
          carregarImagem(e){
              this.arquivoImagem = e.target.files // object array
          },
          guardar(){
              // Warning : e.target.files is an array of objects
              console.log(this.nomeMarca , this.arquivoImagem[0])

              /* axios.post(  <url>,
                              <request_content>,
                              <request_config>
                          )
              */

              /*
                  When we are talking about http requests for the backend
                   of our application that involve sending files,
                   we need to set the body of our request to
                   being of the form-data type.
              */

              // program a form via javascript just like the POSTMAN form

              let formData = new FormData()
              formData.append('nome', this.nomeMarca)
              formData.append('imagem', this.arquivoImagem[0])

              let config = {
                  headers: {
                      'Content-Type': 'multipart/form-data',
                      'Accept': 'application/json'
                  }
              }

              axios.post(this.urlBase, formData, config)
                .then(response => {
                    console.log(response)
                })
                .catch(errors => {
                    console.log(errors)
                })
          }
      }
  }
</script>
Posted by: Guest on September-22-2021
-1

axios response is string how to get resposne headers

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });
Posted by: Guest on July-06-2020

Browse Popular Code Answers by Language