'AWS post request works on postman but not react-native

This is my JS code for the API

export const getUser = async (user) => {
    //Working
    const json = await fetch( "*****/username/getUser", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        user:user
      }),
    })
      .then((res) => {
        return res.json();
      })
      .catch((err) => {
        console.log("Error in getUser: " + err);
      });
    return json;
  };

Here is an attempt to make the request, which unfortunately return the authentication error.

    fetchUser.request("kirolosM")
    .then((result)=>{  
        console.log(result);
    }).catch((err)=>{console.log("Error ",err);})

The error

{
  "message": "Missing Authentication Token"
}

I have tested the API using postman and it is working as expexted.



Solution 1:[1]

Probably useful to compare the request sent from your json code to the one you're sending from postman. It looks like you need to include you auth token in your headers in your request.

Something like

export const getUser = async (user) => {
    //Working
    const json = await fetch( "*****/username/getUser", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Authentication": `Bearer ${token}`
      },
      ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex