0

I am facing a problem. And the solutions that are accepted by the others, those don't work for me./

I have:

return await fetch(url, { method: httpMethod, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'x-test':'try me', }, body: JSON.stringify(bodyObject) }); 

But, it seems like no header is set at all.

Cors-header is set, adding mode:'cors' makes no difference.

Am I missing something?

the request:

Request

Accept is empty and Content-Type nor x-test are nowhere to be found.

..

Edit

..

If i need to create new question, do tell me.

New request request2

So I see that the header is set - thanks to liminal18!-. However, now i want to authorize to an AD.

so now I got:

async callUrl(url, httpMethod, bodyObject) { try { var requestObj = { method: httpMethod, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'x-test':'try me', 'Authorization': 'Basic Uk11bGxlc....' } }; if (bodyObject !== undefined){ requestObj.body = JSON.stringify(bodyObject); } return await fetch(url, requestObj); } catch(error){ console.error(error); } } 

But still get an 401. I know this is not the clean solution for logging in, but just testing..

6
  • you might try if(bodyObject) and let javascript convert the object to a boolean. good job though glad you are making progress. is the 401 do to CORS? CORS can be a little tricky. Commented Jan 13, 2017 at 12:15
  • Your allow headers in the response is just authorization btw Commented Jan 13, 2017 at 12:23
  • options btw should not be returning a 401 maybe you are missing an auth header with the refresh token / access token? Commented Jan 13, 2017 at 12:24
  • well, the first call to the front-end prompts a basic authentication box, when logged in, the proxy continues to the desired site, that same-sites makes calls to the backend, through the same proxy. And I was hoping that I could use the same-credentials, something like 'credentials': 'include'. Hence, trying the authorization shizzle... :) need to set another header or so? Commented Jan 13, 2017 at 12:27
  • Take a look at the requests when logged in and see if there is an auth header. So many things could be wrong. Also I think there is a way to store an auth token maybe use redux and create a session reducer that stores the atuh token and then appends it requests as long ss the user is logged in. Kind of strange to realize I have never dealt with auth in react! Commented Jan 13, 2017 at 12:30

1 Answer 1

1

Are you sure the request you captured is the correct request? if you're using CORS there is an OPTIONS post to get the cors allowed methods before react will post/put which might be what you are looking at and not the actual post or put you intended. Where are the CORS headers set?

Sign up to request clarification or add additional context in comments.

6 Comments

100% sure. The response header has: Access-Control-Allow-Origin:*, maybe I am missing something, but it is only for a Get, does that make a difference?
on the request does the request method match the method sent to fetch?
also is this React.Js or React Native? keep in mind React Native does not use CORS.
is GET an allowed method on the server? you do have to specify both the methods and origins allowed.
yeah I just tried in console and my OPTIONS post lacked the headers however my GET request did have the headers. are you using chrome or firefox as your browser?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.