Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

axios send post data to nodejs api

  1. User

    axios send post data to nodejs api

  2. Assistant

    To send post data from axios to a Node.js API, you can use the following steps:

    1. Import axios in your Node.js API file:

      const axios = require('axios');
      
    2. Create a route to receive the post data:

      app.post('/api/data', (req, res) => {
        console.log(req.body);
        res.send('Data received');
      });
      
    3. In the client-side code, create a function to send the post data using axios:

      const postData = async () => {
        try {
          const response = await axios.post('/api/data', {
            data: 'Data to be sent',
          });
          console.log(response.data);
        } catch (error) {
          console.error(error);
        }
      };
      
    4. Call the postData function when you want to send the data.

      postData();
      

    Note: Make sure to use the correct URL for your API endpoint. If you're running the API locally, the URL will be something like 'http://localhost:3000/api/data'.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.