Home > Enterprise >  Different on postman and React compared to sending it through API?
Different on postman and React compared to sending it through API?

Time:10-23

It's like I'm sitting and working on sending some data from React to the API. It's my own API.

When I use postman, there are no errors that appear at all. But Do I use Browser, it turns out that it makes a 404 error.,

When I use PostMan, my data looks like this:

{
    "email":"[email protected]",
    "name":"test111",
    "passwordValue":"Testswghdsogsdgk@",
    "schoolAccount": false
}

Herewith when I need to post something from React to API, I do it this way. Where I make use of axios and I have also tried to do it without axios.

const [mail, setMail] = useState('');
const [passwordValue, setPasswordValue] = useState('');
  const [name, setName] = useState('');
  const schoolAccount = false;

  const handleSubmit = (e) => {
    e.preventDefault();
    const data = {mail, passwordValue, name, schoolAccount}

    console.log(baseURL, data)

    axios.post(baseURL, data)
    .then(res => res.json())
    .catch(console.log("Error"));
  }

Console.log giv me:

https://localhost:49153/Opret-bruger

{mail: '[email protected]', passwordValue: '12321213321@AKsfkjaf3!asdf', name: 'Hello World Name', schoolAccount: false}

But it is such that this error is reported when I post.

POST https://localhost:49153/Opret-bruger 400

Uncaught (in promise) Error: Request failed with status code 400
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.onloadend (xhr.js:66)

But what is strange is that when I post to the API from React it does not go through but on the other hand I do it from Postman so there are no problems with throwing it into the database.

I would like to have my problem solved in relation to how it can be that I can not post through React like Postman.

I have try to look on: Axios.post doen't work in react js

CodePudding user response:

hello if you look carefully there is a difference between the format to send with PostMan and axios I propose to change the code like this

   const [mail, setMail] = useState('');
const [passwordValue, setPasswordValue] = useState('');
  const [name, setName] = useState('');
  const schoolAccount = false;

  const handleSubmit = (e) => {
    e.preventDefault();
    const data = {"mail":mail, "passwordValue":passwordValue, "name":name, "schoolAccount":schoolAccount}

    console.log(baseURL, data)
    axios.post(baseURL, data)
    .then(res => res.json())
    .catch(console.log("Error"));
  }

CodePudding user response:

the standard format jsonobject {key: value} but in the log console {variable: value} you have to change the format like this

{
    "email":"[email protected]",
    "name":"test111",
    "passwordValue":"Testswghdsogsdgk@",
    "schoolAccount": false
}
  • Related