0

Why do i get this kind of error when trying to send a post request using axios Here is the code:

const params = ({
    "username": username,
    "password": password,
    "firstname": fname,
    "middlename": mname,
    "lastname": lname,
    "address": address,
    "contactnumber": parseInt(contact)
});

function newUser() {
    axios.post("http://localhost:8080/citizen/postCitizen", params,{
        "headers": { 
            "content-type": "application/json" 
        },       
        })
        .then((response)=>{
            console.log(response.data)
        })
        .catch((error)=>{
            console.log(error)
        })
    console.log(params)
}

Here is the error in the console: enter image description here

Jikk
  • 29
  • 4
  • 1
    FYI, you don't need to set the content-type header; Axios does that for you already – Phil Dec 18 '22 at 06:31
  • Try logging the actual response body... `console.warn("response", error.response?.data)`. it might give you some clues about what's wrong – Phil Dec 18 '22 at 06:32
  • 1
    A 415 is `Unsupported Media Type`. It usually means you're sending data in an unexpected format. What type of data does your API expect? Do you have access to the code or documentation? – Phil Dec 18 '22 at 06:34
  • Thank you very much it works now. I just removed the header :D – Jikk Dec 18 '22 at 06:41
  • 1
    What, removing the content-type header? That seems odd and shouldn't have really made any difference – Phil Dec 18 '22 at 06:42
  • Does this answer your question? [Pass Data to Service in Axios](https://stackoverflow.com/questions/68643330/pass-data-to-service-in-axios) – Phil Dec 18 '22 at 06:43
  • yes removing the header solves the problem. idk how but somehow it worked. thanks – Jikk Dec 18 '22 at 15:51

0 Answers0