Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

604
Views
how to submit data from a data form in axios

I am trying to send a form to an endpoint but I get an error forbidden 403, and I already tried with an api test and the endpoint works perfectly

const SendData = (event) => {
    event.preventDefault();
    let respuestasUSer = new FormData();
    respuestasUSer.set('curso', state.curso)
    respuestasUSer.set('nombre', state.nombre)
    respuestasUSer.set('q1', state.Pregunta1)
    respuestasUSer.set('q2', state.Pregunta2)
    respuestasUSer.set('q3', state.Pregunta3)
    respuestasUSer.set('q4', state.Pregunta4)
    respuestasUSer.set('q5', state.Pregunta5)
    respuestasUSer.set('q6', state.Pregunta6)
    respuestasUSer.set('q7', state.Pregunta7)
    respuestasUSer.set('q8', state.Pregunta8)
    axios({
        method: "post",
        url: "https://covid.cit0.com/guardar/encuesta/",
        data: respuestasUSer,
        headers: { "Content-Type": "multipart/form-data" },
      })
        .then(function (response) {
          //handle success
          console.log(response);
        })
        .catch(function (response) {
          //handle error
          console.log(response);
        });   
} 

In the api test I do it like this and it works for me, how could I pass this same content to axios? enter image description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you are using react as front end

first you set onchange event in input text field like

<form>
<input type="text" name="first_name" placeholder="first name" onChange={handlechange} />

Create

define that handlechange function like

function handlechange (e) {
        const {name,value} = e.target
        setstate({...state,[name]:value}) 

}

//before that want to define state like const [state, setstate] = useState({})

then define handleclick function like

    function handleclick (e) {
            e.preventDefault()
            
             axios.post('/super_admin/newuser',state).then((res)=>{
                
console.log(res.data)
                
            }).catch(err=>{

console.log(err.response.data)

})
    }

here is the full code

import {useState, } from 'react'
import axios from 'axios'

export default function Newuser() {

const [state, setstate] = useState({})

function handlechange (e) {
        const {name,value} = e.target
        setstate({...state,[name]:value})
    }

function handleclick (e) {
                e.preventDefault()
                
                 axios.post('/your api',state).then((res)=>{
                    
    console.log(res.data)
                    
                }).catch(err=>{
    
    console.log(err.response.data)
    
    })
        }

return (

<div>
<form>
<input type="text" name="first_name" placeholder="first name" onChange={handlechange} />
<button onClick={handleclick}>Create</button>
</form>
</div>
)

}
about 4 years ago · Juan Pablo Isaza Report

0

for sending form data you can edit handleclick function to something like this

    function handleclick (e) {
       e.preventDefault()

   const fd = new FormData()

                fd.append('first_name',state.first_name)

                 axios.post('/your api',fd).then((res)=>{
                    
    console.log(res.data)
                    
                }).catch(err=>{
    
    console.log(err.response.data)
    
    })
        }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!