last day (16 days later) » 

8:03 PM
0
A: Angular 6 Http request fails with authorization headers

Aravindan VeYou need to configure CORS on your tomcat server. You need to tell tomcat which headers the application is allowed to send, so it can include it in the preflight response: <init-param> <param-name>cors.allowed.headers</param-name> <param-value>Authorization,Content-Type,...</param-value> <

 
Can you help me in where I have to puth theese lines?
 
Hmm I've never used tomcat, but you will need to find the config file, what is your OS?
 
I'm using windows
 
do you know the tomcat installation directory? If yes then, TOMCAT-HOME/conf/server.xml and TOMCAT-HOME/conf/web.xml are supposedly the config files. You may have to search both of them for cors.allowed.methods
 
Yes, I have to put it on web.xml?
 
8:03 PM
I'm not sure. Like I said, never used tomcat and a little rusty with windows. So you will have to open both the files and do a text search. web.xml sounds like a good place to start.
 
My web.xml have a lots of init params
 
can you find cors.allowed.methods
do Ctrl+F and see
 
no matches
 
now can you search server.xml
 
I have it
But anything matches
 
8:13 PM
sorry, I dont understand
no matches?
 
I can't find anything with cors.allowed.methods on server.xml
 
okay
add this to your web.xml file
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>

<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET, HEAD, POST, PUT, DELETE, OPTIONS</param-value>
</init-param>

<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials,Authorization</param-value>
then restart your tomcat and see if it works.
 
but where? in my web.xms there are 4500 lines
 
also make sure you remember where you put this, so you can remove it if something goes wrong..
find one of the <filter></filter> blocks
put it next to them
 
8:26 PM
does it work after restarting tomcat?
 
no I have the same problem
but the problem persist only when I make the call with angular
Rest client read it well
 
8:43 PM
hmm rest client don't abide by CORS rules
browsers, for security reasons do. very strictly
You are still getting the CORS error?
If you are then this is the only way. to figure out how to configure tomcat to return CORS headers properly. Or change your server :)
tomcat config seems needlessly complex
 
Do you think I can use jboss server? becouse I've written my code on java and I don't want to use javascript
 
before you do that, can you send me the response headers for your failing request?
you will find it in thee network tab in the developer console
 
access-control-allow-origin:
*
access-control-allow-credentials:
true
access-control-allow-headers:
Origin, X-Requested-With, Content-Type, Accept, Authorization
access-control-allow-methods:
GET,HEAD,OPTIONS,POST,PUT,DELETE
content-length:
0
date:
Sun, 02 Dec 2018 20:53:51 GMT
 
8:58 PM
Hmm okay tomcat is returning Authorization in access-control-allow-headers
 
so the problem is on tomcat or on my client?
 
so theoretically it should work. Your browser still says No 'Access-Control-Allow-Origin' header is present on the requested resource.?
which browser are you using?
 
i'm using chrome in debug mode of angular
 
now I'm leaning towards the problem being client side. since I cant see this first hand, its hard to say
can you also set the request headers?
maybe some headers are missing from the request, since you are manually building them
 
the request headers is my problem... the http call works without authorization header, when I add authorization header the error appears
without headers the request works
I've write that in my answer
 
9:04 PM
hmm yeah, once you add the authorization header, your OPTIONS request fails am i right?
 
yes sure
 
can you send me the request headers for that request
 
I've tryied to search this problem but... nobody had it too...
 
from your network tab i mean
 
Sorry I don't undertand... wich request?
from angular?
 
9:05 PM
if you open your network tab, do you see a request in red?
 
network tab?
 
in your developer console
 
sorry I can't understand what do you mean
 
Ctrl + Shift + J
in your chrome
your console opens up right
 
ah ok from chrome
 
9:07 PM
yes :)
and next to the console tab, you'll see a tab calledNetwork
you will have to reload your page to see the requests
once you do that, try initiating the POST request in question
 
you see it fail, and i'll appear red in the network tab
yeah you have to click login
 
Yes and it is the output
it says 200
 
Okay its definitely tomcat
its not returning access-control-allow-headers
 
9:22 PM
oh yeah i saw that too.
btw how are you restarting tomcat?
 
I restart it from my IDE
 
ah, okay then there might be a second web.xml file
specifically for your application
 
Yes there is
 
can you insert the same code there too
the <filter></filter> and the <filter-mapping> part..
 
I'm trying
Hehe goal
 
9:31 PM
haha
wait whats the error now
 
But the request cen't reach my server... mmh
now my server prduce unauthenticated result
is correct
 
you are getting a 403 forbidden
ah okay
 
but I can't read request from server
 
as in it's not reacing your backend application?
reaching*
can you show me those 4 errors in your chrome console?
 
hmm there is the same error
 
9:36 PM
it could also be java
are you using spring framework?
a lot of people seem to be facing the same issue with spring
 
what framework are you using on the backend
 
simple java
 
there are also some people who are saying this got fixed after they added Content-Type header to their request
can you try that?
 
which request?
angular true?
 
9:42 PM
yup like this new HttpHeaders({ 'Content-Type': 'application/json' }).append(...
 
the problem persist
but request headers reporten on chrome doesn't correspond
 
meaning?
 
I can't find content type
 
im not sure what else there is left to try
you can look at the tomcat logs to see what tomcat is reporting
should be somewhere in $TOMCAT_HOME/logs
the file may be huge, so look at the last few lines
 
I have catalina console that report my output
 
9:53 PM
ah okay
what does it say
does anything get printed when you make the request?
 
I'm cheching if I recive request
 
cool
 
I the request doesn't reach tomcat
 
What I have to do
 
10:05 PM
actually use this link
and click send request and check the response you're getting.
 
Sending OPTIONS request to localhost:8095
, with credentials
Fired XHR event: readystatechange
Fired XHR event: error

XHR status: 0
XHR status text:
Fired XHR event: loadend
 
what about if you add http:// before localhost
on the page i sent you
btw it's really late here in Bangalore. I have to work tomorrow. sorry, i need to go to sleep :D
 
How I can take the chat?
 
Take the chat as in?
 
sorry, to save the chat
the link remain?
 
Thank you, good night
 
And I would suggest you edit your question to include these new findings, for example highlight the fact that is is an Angular-Tomcat-Java CORS problem. people who have faced this will be able to find it more easily
you're welcome :)
 
with credentials
Fired XHR event: loadstart
Fired XHR event: readystatechange
Fired XHR event: readystatechange
Fired XHR event: progress
Fired XHR event: readystatechange
Fired XHR event: load

XHR status: 200
XHR status text: OK
XHR exposed response headers:

Cache-Control: no-cache
Content-Type: application/json
Expires: Sun, 02 Dec 2018 22:11:39 GMT



CORS Request

POST http://server.cors-api.appspot.com/server?id=5537254&enable=true&status=200&credentials=true
Origin: http://client.cors-api.appspot.com
Here is the response
Sorry is the test example... i'm tired too
 
haha yeah i just noticed
cool, anyways all the best. word of advice, move to the javascript world. nodejs is a lot simpler. :D
and nginx server
 

  last day (16 days later) »