Home > Net >  cant fetch data with a bearer token
cant fetch data with a bearer token

Time:11-14

i have been trying over a 3 days in order to get the data from a bearer token API but i couldnt

here is my code:

const token = "MYOTKEN";
function App() {

  const body = {
    product: {
   somerandombody,
    },
  };
axios
    .get("http://upayments-studycase-api.herokuapp.com/api/products", body, {
      headers: {
        "Content-Type": "application/json",
        Authorization: "Bearer "   token,
      },
    })
    .then((response) => {
      console.log("response", response.data);
    })
    .catch((error) => {
      console.log("error", error.response);
    });

here is the error im getting:

error 
{data: {…}, status: 401, statusText: 'Unauthorized', headers: AxiosHeaders, config: {…}, …}
config
: 
{transitional: {…}, transformRequest: Array(1), transformResponse: Array(1), timeout: 0, adapter: ƒ, …}
data
: 
{message: 'not authorized', stack: null}
headers
: 
AxiosHeaders {content-length: '41', content-type: 'application/json; charset=utf-8', Symbol(defaults): null}
request
: 
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status
: 
401
statusText
: 
"Unauthorized"
[[Prototype]]
: 
Object

so, what I want is to fetch the data from the API with a bearer token. What am I doing wrong?

CodePudding user response:

The Error comes from the server . You need to recheck the logic for accessing the endpoint at the server side .

CodePudding user response:

Try to test that endpoint by using API consuming software, for example Postman, so you can see if the server is the fault.

CodePudding user response:

there might be two reasons for this : 1.Token that u are using is probably expired, or 2.You don't have permission to access endpoint.

  • Related