I have tried to use Sanic_CORS in exception handler like error 404,
but I received CORS error
Access to XMLHttpRequest at 'http://backend:port/endpoint' from origin 'http://react_app:port' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
There are no problems with valid endpoints.
My error handler
#app.exception(NotFound)
async def ignore_404s(request, exception):
return response.json({'message': "Oops, server error"},
status=404)
How can I implement Sanic_CORS for error handlers? Any ideas?
One option would be to just add the header into your response. Albeit, this is not using the sanic-cors package, but you really do not need it here.
#app.exception(NotFound)
async def ignore_404s(request, exception):
return response.json(
{'message': "Oops, server error"},
status=404,
headers={"Access-Control-Allow-Origin": "*"}
)
Related
I would like to know if the 'XMLHttpRequest Error' thrown by http package is always due to missing CORS policy header?
The expection thrown in this instance is of Type ClientException - the only things that I can read is the message (XMLHttpRequest Error) and uri. So how would I know it is because of the above.
I get intermittent(and infrequent) errors in the logs, and the CORS policy header is correctly set on the server.
I want to rule out any other causes of XMLHttpRequest Error. Any ideas?
I am using http request (post) to an api endpoint.
I pass the correct parameters for authentication on msg.payload {"username:"xxx","password":"password"}. The server return the statusCode = 302 and I can't read the set-cookie header.
How to solve this problem?
I was using the node-red-contrib-https 1.0.0. I changed to the HTTP Request Node that comes with node-red, import the tls certificate and everything works.
I have a GET /v2/developer/jhUxad8 endpoint which get developer from database and serialize it to json.
What http status code should I return when serialization fail?
https://www.codetinkerer.com/2015/12/04/choosing-an-http-status-code.html
What http status code should I return when serialization fail?
If I'm understanding you correctly, the server understands the request, and is trying to fulfill it, but because of a data problem is unable to do so.
500 Internal Server Error
The 500 (Internal Server Error) status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.
Well, I've tried to send a post request from HttpClient class in Ionic3, in the next code fragment I show the function in TypeScript.
constructor(public http: HttpClient, ...){ ... }
loginAndGetUserData(email0, password0): any
{
return this.http.post
(
'http://xxx.a_api/api/login.json',
{
email: email0,
password: password0
},
{
headers:
{
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods':'POST, GET, OPTIONS, PUT',
'Content-Type':'application/json',
'Accept':'application/json'
}
}
);
}
Then, when I call this method I receive an error with undefined code as response.
ErrorCode: undefined
Http failure response for (unknown url): 0 Unknown Error
Capture image of device
In Android logs, after the function call, I get this message:
08-09 21:16:31.649 19292-19292/io.ionic.starter D/SystemWebChromeClient: http://localhost:8080/: Line 0 : XMLHttpRequest cannot load http://xxx.a_api/api/login.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.
08-09 21:16:31.649 19292-19292/io.ionic.starter I/chromium: [INFO:CONSOLE(0)] "XMLHttpRequest cannot load http://xxx.a_api/api/login.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.", source: http://localhost:8080/ (0)
And in my PHP code I tried to put 'Access-Control-Allow-Origin:*' in header, but It receive empty data from the client. In some Ionic forum some people say that I have to modify ionic.config.json file putting proxies attribute with path and proxyUrl, but neither It resolve my issue.proxies attribute, in ionic.config.json file, looks like:
"proxies":
[
{
"path": "/api",
"proxyUrl": "http://xxx.a_api/api"
}
]
Well, I don't know what to do, I'm sure that It's a client problem, 'cause the server receive empty data.
Thanks in advance.
This is CORS issue. You will need to handle it on server side. For testing purpose, you can use Allow-Control-Allow-Origin: * plugin from chrome web store. By enabling this plugin your issue will be resolved.
I have the following code that is trying to invoke and api gateway function called endsession in swift:
client.endsessionPost(body: pleasegod!).continue ({ (task:AWSTask) -> Any? in
if let error = task.error {
print("Error occurred: \(error)")
return nil
}
if let result = task.result {
print("IT WORKED")
// Do something with result
}
return nil
})
It runs and completes but task error occurs and the print statement of what the error is says:
Error occurred: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x618000056e60 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=(null)/endsession, NSErrorFailingURLKey=(null)/endsession, NSLocalizedDescription=unsupported URL}
It seems to be saying the endpoint url provided is bad. The endpoint url was just copied and pasted from the endpoint of the api gateway deployment so I am not sure why that would be an unsupported url.
The endpoint has a format like so:
https://kco37e2fsa.execute-api.us-west-1.amazonaws.com/TheStage
When I visit this endpoint in a browser I notice it says:
{"message":"Missing Authentication Token"}
Which is strange since both my api gateway resources have Authorization set to None and API Key set to not required on all their methods. Is this missing authentication token a clue as to why I am getting unsupported URL errors in swift when invoking the API??
The url https://xxxxxxxxxx.execute-api.us-west-1.amazonaws.com/TheStage will only work if you have a GET method configured on the root resource, and if that is deployed to a stage called TheStage. (No sure if the API Id and/or stage are real). A browser will send a GET request if you just navigate to the URL.
I would recommend a tool like Postman for testing API calls; you can configure parameters, body, HTTP method, auth, etc.