My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

What is the right way to POST an auth form using isomorphic-fetch?

Ajar's photo
Ajar
·Jun 8, 2016

Hi all,

I'm using the following code to send an ajax request from a react/redux app calling an express POST endpoint, based on Mern.io I'm quite sure the problem is not on the server side since I've tested this endpoint with Postman and it works correctly. So this is the code I'm using on the client, and it returns {message: "Missing credentials"} from passport authentication... again, no problem on the server...

I am using bodyparser correctly, logging out the req.body.email and req.body.password - they are printed correctly using Postman, but initiating this call using isomorphic-fetch with the following code is printing the following req.body on the server...

{ '------WebKitFormBoundaryKtn3SHumfq4YBeZ1\r\nContent-Disposition: form-data; name': '"email"\r\n\r\ntest@domain.com\r\n------WebKitFormBoundaryKtn3SHumfq4YBeZ1\r\nContent-Disposition: form-data; name="password"\r\n\r\ntestPassword\r\n------WebKitFormBoundaryKtn3SHumfq4YBeZ1--\r\n' }


import fetch    from 'isomorphic-fetch'
import FormData from 'form-data'

export function signup(payload) {

    return (dispatch) => {

        let formData = new FormData()
        formData.append('email',payload.email)
        formData.append('password',payload.password)

        fetch(`${baseURL}/signup`, {
          method: 'post',
          body: formData,
          headers: new Headers({
            'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
            'Accept': 'application/json, application/xml, text/plain, text/html, *.*'
          }),
        })
        .then((res) => res.json())
        .then((res) => {
            console.log('Fetch signup result:',res)
            console.log('dispatch:',dispatch)
            dispatch({
                type        : Auth_Actions.SignUp_Success,
                userObject  : res
            })
        })
        .catch((err)=>{
            console.error('Fetch signup ERROR:',err)
            dispatch({
                type        : Auth_Actions.SignUp_Fail,
                userObject  : err
            })
        });

    }
}