How is an HTTP POST request made in node.js?

后端 未结 21 2798
南方客
南方客 2020-11-21 23:54

How can I make an outbound HTTP POST request, with data, in node.js?

21条回答
  •  我在风中等你
    2020-11-22 00:19

    Axios is a promise based HTTP client for the browser and Node.js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.

    const axios = require('axios');
    
            var dataToPost = {
              email: "your email",
              password: "your password"
            };
    
            let axiosConfiguration = {
              headers: {
                  'Content-Type': 'application/json;charset=UTF-8',
                  "Access-Control-Allow-Origin": "*",
              }
            };
    
            axios.post('endpoint or url', dataToPost, axiosConfiguration)
            .then((res) => {
              console.log("Response: ", res);
            })
            .catch((err) => {
              console.log("error: ", err);
            })
    

提交回复
热议问题