Fetch request blocked by CORS policy [duplicate] - api

This question already has answers here:
No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API
(26 answers)
Closed 11 months ago.
So I tried to fetch up some Clash Royal data, which works perfectly on Postman, but when I try to use it in my localhost server, this message appears:
This is my Code:
I have tried several headers, all of which do nothing. All solutions on the internet say that I need to change the API settings, which is impossible since I am (obviously) not the owner of the API. Am I doing something wrong?
I expected to get the correct json data and tried several headers, even the one Postman uses.

You can use https://allorigins.win/, just change your URL to https://api.allorigins.win/raw?url=https://api.clashroyale.com/v1/players/%23C8UVYUL90

When the server sends a request, there is no cors check.
Cors error occurred when origin is different in the web browser.
In this situation, your request is from origin: 192.168.2.36:5000 to: https://api.clashroyale.com. Origin is different.
Thus, if clashroyale server is not yours, you cannot put reponse header: Allow-Access-Control-Origin-Header: * for allowing access from 192.168.2.36:5000. So you should process this request in your own server not in your website!

Related

CSRF and CORS: Why allow the request to happen if we know there will be a cors error?

I am confused by why the cors package allows the request to be processed even if the origin in the request header isn't white-listed. For example, res.status(202).send(await User.find()) returns a response with status code 202, but the data can't be loaded in the Chrome console.
Also, doesn't the browser send preflight OPTIONS requests to know what's allowed; why would it send cookies/credentials along a request with a disallowed origin?
Edit: Tried a post request on jsfiddle and the post request doesn't happen server side. When I said "why the cors package allows" it would be better to say why the browser allows.
CORS is enforced in the browser, not in your server. The server participates in setting headers that the browser can then use to determine whether the request should be allowed or not. But, it is the browser that ultimately decides whether the CORS request satisfies the requirements or not and the result should be passed through to the Javascript in the browser.
Thus, the request is sent to the server, response is received and THEN the browser decides whether the Javascript in the page is allowed to see the result or not.
In some cases where the request is likely to have side effects on the server (based on a set of criteria in the request), the browser will send a pre-flight request to get just the CORS info first.

Not able to retrieve response for a GET request via Karate [duplicate]

This question already has an answer here:
Passing JSON request starting with the character [ gives the error: invalid request format with GET operation [duplicate]
(1 answer)
Closed 2 years ago.
I am getting an error as below when I execute my scenario
Feature: sample karate test script for ws+
Background:
url 'https://pluss-nav2.ekstern.acos/api/api/register/'
configure headers = { Accept: 'application/json' }
configure ssl = true
header Authorization = 'Bearer token'
Scenario: get all users and then get the first user by id
Given path 'sakmaler'
When method get
Then status 200
When method gets executed, I get the below exception.
com.intuit.karate.exception.KarateException:
com.intuit.karate.exception.KarateException: wsplussnav.feature:14 -
java.net.UnknownHostException: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server (pluss-nav2.ekstern.acos)
Note: I get this exception only when I execute test on this url. This url does not have any proper domain like .net or .com
I tested in postman, and it works.
I tested in powershell command, and it works.
Any idea how to re-solve this error in Karate?
Most likely you are behind a corporate proxy, which is why it works in P*stman.
Refer the docs: https://github.com/intuit/karate#configure
karate.configure('proxy','http://myhost:80');
EDIT: for others landing here, besides the fact that an HTTP proxy may be in the picture - another place where Karate behaves a bit differently than Postman is that Karate does not auto-send an Accept header by default.

Use AJAX to GET smart_collections.json

Within a Shopify store I am making several AJAX calls and only one returns a 401 Unauthorized. The AJAX url is https://{{key}}:{{password}}#stand-around-creations.myshopify.com/admin/smart_collections.json?limit=250&fields=id,title,handle,image and it returns this:
XMLHttpRequest cannot load https://stand-around-creations.myshopify.com/admin/smart_collections.json?limit=250&fields=id,title,handle,image.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://stand-around-creations.myshopify.com' is therefore not allowed access. The response had HTTP status code 401.
I am using a private app with read access only for products, variants and collections. I know this is an authentication issue but how to solve to me isnt really clear. I have never used Basic Auth, OAuth or OAuth 2 and only have a very basic understanding of the concept. I tried searching the Shopify forums for a solution but couldn't find a solution. So how can I solve this issue or what am I doing wrong?

Postman Resolving "Invalid CORS request" for a POST Request

I've just started using Postman to test an API I am integrating to.
I have the following error that keeps showing up
Invalid CORS request
Note the following:
The API uses Bearer token authentication(OAuth2). I have this
working without a problem.
I do get the bearer token successfully, assign it to an Environment variable and then attempt to use it for the RESTful operations.
The problem is in the subsequent RESTful operation that uses the token.
When I use an old token (through a POST operation), it rightfully
tells me that it is expired and not authorized.
When I then generate a new one and try to run the restful call, it gives me that Invalid CORS request error.
Using cURL, I have no issues. But I am frustrated by Postman.
What I have found so far:
Using postman with Http POST requests - I don't get the part in bold
Just in case anybody else has this same problem, here is how to solve
it. Go to https://www.getpostman.com/docs/capture in your chrome
browser. Click on interceptor extension and then choose add to
chrome. Once it is added there is a new icon top right of both the
browser and postman that looks like a traffic light. In postman click
this and it turns green. Then add a header to every request going to
third light. Every header consists of the header name and a value.
Start typing over the header name and a list of allowed http headers
comes up. Choose "Origin". In the cell for value simply type the
full URL of your server. (Do not forget the 'http://' or 'https://').
What is the expected response to an invalid CORS request? - Best explanation I have seen so far on CORS errors.
The other material speaks about Access-Control-Allow-Method header, preflight requests
... and there is an illustrative Apache Tomcat flowchart of the CORS flow.
Here's the answer you found again:
Just in case anybody else has this same problem, here is how to solve it. Go to https://www.getpostman.com/docs/capture in your chrome browser. Click on interceptor extension and then choose add to chrome. Once it is added there is a new icon top right of both the browser and postman that looks like a traffic light. In postman click this and it turns green.
... With the bit in bold translated:
Then add a header to your request. The header Key should be "Origin" and the header Value should be the full URL of your server (Do not forget the http:// or https://).
Note that Chrome/Postman won't allow you to add a Header with a Key of Origin without the Interceptor plugin.
Also note that at least on my system the Interceptor icon no longer looks like a traffic light.
If your back-end service side code checks for origin of the request (just to avoid CORS attack) you may face this issues when testing your Rest API through postman.
How to Resolve this .?
You need to install a Chrome plugin called Postman Interceptor (https://chrome.google.com/webstore/detail/postman-interceptor/aicmkgpgakddgnaphhhpliifpcfhicfo?hl=en).
After successfully installing this plugin , in you Postman client you can see small icon called Postman Interceptor , you need to toggle it to turn it on.
Now you can add a Request header as below
RequestHeader Key "Origin"
RequestHeader Value "your application base URL"
Check this image
Now you should be able to over come CORS issues you are facing
Cheers !!
Just avoid using browser/chrome postman plugin. Use the desktop application instead!
Seems our server is seeing from a Postman manual HTTP POST that the orgin is invalid b/c its coming from Postman as "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop"
Not sure why or how to resolve on client/Postman side. Seems our server is correclty rejecting it as is though and issuing a 403.
Value of "Origin" header set in Postman request should be allowed in API backend. For example, using Spring Boot for API should have next:
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Value("${cors.allowedOrigins}")
private String allowedOrigins;
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(allowedOrigins)
.allowedMethods("*")
.allowedHeaders("*");
}
}
where allowedOrigins is set using application.properties property cors.allowedOrigins having comma separated list of allowed origins, eg:
cors.allowedOrings=http://localhost:8080,http://example.com
and set 'Origin' value in Postman to any url from cors.allowedOrigins
I was getting this error when testing my APIs on the postman. Even after meticulously configuring my cors. So I used Insomnia instead of Postman and it works fine. I guess sometimes postman is the problem as it needs some extra effort.
You can try new version of PostMan. To me it works after upgraded postman version from 5.5.5 to 7.36.5

Cross Domain Error Worklight Adapter Request

I'm trying to make a GET request from an iPhone app(built on Worklight), to a remote worklight adapter on a remote server.
I have generated the authorization request header using the following
http://remote-url:port/project/authorization/v1/testtoken
However, I keep getting the following cross domain error:
XMLHttpRequest cannot load https://remote-url/project/adapters/call/mq/getAccounts/22. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myapp-url:port' is therefore not allowed access.
But, when I try do the same GET request using an API tool like Postman, I receive the results no issues on my browser.
First - testtoken is only available from the MobileFirst Studio development environment... you need to take this into consideration.
Second, you're likely using Chrome and Crome disallows CORS by default. Change your browser settings and/or try in a different browser.