How do i make a post request with my token coming from local storage

Having problems getting this to work. although it works when tested with postman

export const addAdmin = (values) => async (dispatch) => {


var add = localStorage.getItem('user');

const res = await fetch('https://capitalluxurydev.xyz/v1/admin/createadmin', {
    method: 'POST',
    body: JSON.stringify(values),
    headers: {
        Authorization: add,
        'Content-Type': 'application/json; charset=utf-8',
    },
    credentials: 'same-origin',
});
try {
    const data = await res.json();
    console.log(data);

    return;
} catch (error) {
    console.log(error);

    return;
}

};

I keep getting a response of invalid JSON construct

    {data: null
error: "invalid json construct"
message: ""
statuscode: 422
}

But works perfectly on https://hoppscotch.io/

10 thoughts on “How do i make a post request with my token coming from local storage”

  1. I am Brazilian and therefore I speak Portuguese, but I will use the translator to try to help you.

    O postman coloca automaticamente na Authorization o prefixo Bearer [your_yoken]. Tente colocar algo como:

        headers: {
            Authorization: `Bearer ${add}`,
            'Content-Type': 'application/json; charset=utf-8',
        }
    
    Reply

Leave a Comment