-2

Hi I need to convert JSON to form-data or x-www-forum-urlencoded and I am unsure how to do this using JSON.stringify(). Would anyone be able to help me? I'm using react native and strapi headless CMS in order to try and get authentication set up for my application but I'm getting a bad request 400 error.

Code:

const auth = useMemo( () => ({
    login: async (email, password) => {
      const {data} = await axios.post('http://192.168.0.11:1337/auth/local', {
        identifer: email,
        password,
      });

Error Code

Django
  • 11
  • 2
  • https://stackoverflow.com/questions/22783108/convert-js-object-to-form-data, can find your solution here – Gopi krishna Apr 05 '21 at 17:22
  • Does this answer your question? [Convert JS Object to form data](https://stackoverflow.com/questions/22783108/convert-js-object-to-form-data) – evolutionxbox Apr 05 '21 at 17:28

1 Answers1

-1

You could try something like this:

const auth = useMemo(() => ({
    login: async (email, password) => {
      const bodyFormData = new FormData();
      bodyFormData.append('identifer', email);
      bodyFormData.append('password', password);

      const {data} = await axios({
        method: 'POST',
        url: 'http://192.168.0.11:1337/auth/local',
        data: bodyFormData,
        headers: { "Content-Type": "multipart/form-data" },
      });

Here's some more info: axios post request to send form data

Chris Sandvik
  • 1,787
  • 9
  • 19
  • Unfortunately this is not working and I am still getting a 400 error. – Django Apr 05 '21 at 18:03
  • What error are you getting on your backend? – Chris Sandvik Apr 05 '21 at 18:08
  • On the strapi backend its saying "POST /auth/local (8ms) 400" meaning that the server couldnt take the request. I've checked all my code multiple times and can't see any errors. I was following a video on YouTube which was going well and I got the registration working fine a similar way but I keep getting an 400 error everytime I try login with valid credentials. I updated the post to show the error image I'm getting on VSCode frontend. – Django Apr 05 '21 at 18:18