Response for Preflight occuring only for application/json - spring

I have been trying to do a post request from angular 5 to my spring mvc backend. However, I am getting the Response for preflight is inavlid(redirect) error message. I have been looking around for solution since 4 days but with no success. I have tried all suggestions setting the content-type headers. While doing that, I am observing one unusual thing. The preflight error is occuring only when I am setting the content-type header is set to application/json. If I set the header to text/plain the preflight error is gone but 415 unsupported media type error is being displayed. I have been really digging up stuff to get some help. Any help or suggestions are much appreciated.

Using chrome, look at Developer tools - console and see the exact error.
Your service is not setting preflight response correctly for your OPTIONS request.
How can i verify if my service is CORS ready
use PostMan or RestClient and make an OPTIONS request with nothing else (no auth, nothing)
Look for Response headers for OPTIONS request from Step 1.
Response headers may look like this
Access-Control-Allow-Origin: http://exampledomain.comg
Access-Control-Allow-Methods: GET, POST (or whatever methods your want to allow)
Access-Control-Allow-Headers: Content-Type

Add crossOrigin Annotation at your controller, if you have more one controller then add on all controller
#CrossOrigin(origins = "*")
#RestController

Related

Can Access-Control-Request-Headers be disabled in Axios?

I am wondering if this header is added by Axios or if it is added by the browser... to see if I can disable it in the client or must add support for it in the server.
I added a custom token x-access-token and I'm having CORS issues.
Adding to Jon's comment: The Access-Control-Request-Headers header is added by the browser: When the client application desires to make a request (via fetch or XMLHttpRequest) that includes a "non-standard" header like x-access-token, the browser first makes a preflight request with a header like Access-Control-Request-Headers: x-access-token, and only if the preflight response contains Access-Control-Allow-Headers: x-access-token will it make the desired request.
It is your server's duty to produce the correct preflight response.

What does "Response to preflight request doesn't pass access control check" mean?

I have receive the following response when trying to access an API via an ajax request in Chrome:
"Failed to load http://localhost:1880/api_resource: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://localhost:3000 is therefore not allowed access."
As you can see from the message, both client and API are running locally.
I understand that this situation relates to a CORS cross origin request. I see that there are similar questions about this on stack overflow, but from those answers I do not understand what the message is telling me and where it comes from.
Specifically I understand that the response header "Access-Control-Allow-Origin" must be set (typically to '*') to allow access to the API from a different domain to the one on which the API is being served. But the message seems to relate to the request and not the response, and as far as I am aware, no request ever reaches the API.
What is a preflight request and how is it failing?
As I now understand it, modern browsers will issue a 'preflight' request before the actual cross origin request. This preflight request uses the 'OPTIONS' HTTP verb along with the CORS headers Access-Control-Request-Method and Access-Control-Request-Headers to which it expects to see a response with valid Access-Control-Allow-Origin in the header that indicates that the server understands the CORS protocol and will allow the actual (GET/POST/PUT) request.
The message "Response to preflight request doesn't pass access control check" means that the browser did not see a valid "Access-Control-Allow-Origin" header in the Options response.
In my case this was because the server (implementing a REST API) was set up to respond correctly to PUT and POST requests but not setup to respond to OPTIONS requests with the CORS headers.
in my case the problem was for my website address, i'm calling all apis from the same server but i got this error.
my website address is sateh.ir
so im my ajax request i set the url: http://sateh.ir/api/...
after getting this error and working on it for some hours, i got that i had to set ajax url to: http://www.sateh.ir/api/...
i dont know why my website cant understand that i'm calling api from the same server if i dont put 'www', but that was my problem at all.

CORS: No 'Access-Control-Allow-Origin' header is present even though response is 200?

I am trying to make a cross domain request from my React app (localhost:3000) to my Laravel PHP app (localhost:8000). I believe I have the back end set up to accept cross domain requests. I used this: https://github.com/barryvdh/laravel-cors
I seem to have all the parameters from this answer (https://stackoverflow.com/a/38087435/1555312), so I don't get why mine doesn't work. I actually see a 200 response + the expected body when I use the chrome console.
Here is the error I see in my console:
Failed to load http://localhost:8000/api/v1/upload-sessions: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
Here is how my POST request looks like:
OPTIONS request:
POST request:
Expected response is sent, even though it complains about the CORS issue:
You need to set Access-Control-Allow-... on the response, not the request.
Once you remove that from the request, you probably won't need a pre-flight, so the OPTIONS request won't happen.

Why CORS still not works in WebAPI?

I tried to set everything in web config and I see correct response headers for OPTIONS request made by browser, but why it is still not allowed? I am trying to use WebAPI post requests with AngularJS 1.2.
Try:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Access-Control-Allow-Headers, Content-Type
Accept Access-Control-Allow-Methods:DELETE, PUT, POST, GET
Access-Control-Allow-Origin:*

JMeter HTTP Request: Always Sending GET Method

All,
Every HTTP Request I make to my test REST Service is sent with the method set to GET. Tomcat rejects with a 405 - Unsupported Method. Doesn't matter what I change it to (POST, PUT, etc) Jmeter always sends a GET.
I set up the simplest possible test case by creating a Threadgroup with an HTTP Request Sampler and a View Results Tree. I send a JSON body to the REST Services which just echos back the request along with an ID. Works great with Google's REST Client UI.
Here is the result from the View Results Tree:
Response code: 405
Response message: Method Not Allowed
Response headers:
HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Allow: POST
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1045
Date: Fri, 18 Jul 2014 21:39:27 GMT
Here is the RequestMapping from my REST Service
#RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
Here are some screenshots from my results. I wonder why there are two URI's below the HTTP Request in the tree? Notice the POST request looks correct.
Since the right answer is not provided yet: It's the "Follow Redirects" Option that causes this behavoir under certain circumstances.
see http://www.sqaforums.com/showflat.php?Cat=0&Number=687068&Main=675937
Try to end the 'Path' value of HTTP Request with '/'. It has to remove the GET result in View Results Tree.
I had the same problem. I tried everything also I read this question and all answers before find the thing that worked for me.
Content-Type should be application/json. It can not be text/html.
Set that in HTTP Header Manager. I assume you have set authentication details correctly.
We need to have three things properly set.
Content type which will be application/json
set the endpoint correctly in the path ,which you can see in soup ui
Check the port number on which the api wil get run on [All this u can first check on soupui and then try running the same in jmeter

Resources