CORS Restful Webservice getting blocked - java

Seems like there is a lot of CORS stuff on here. A million ways to handle it.
Converting over an app to Ionic. Because of WKWebView
This seems to run the code as local host on the ios. Which in tern means I need to handle CORS.
Followed this example.
https://amodernstory.com/2014/12/27/using-cors-headers-with-java-example/
At this point its just the web.xml has the CorsFilter setup. (defaults)
I thought I was going to have to do something in the code, but nothing I have added seems to make any difference.
What happens, is I login, and I trace that through and that seems to work fine.
Then the code pulls the users profile info. This is a seperate web service call.
That is getting blocked.
polyfills.js:3 POST [webserviceURL]/user/getUser 403 (Forbidden)
(index):1 Failed to load [webserviceURL]/user/getUser:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8100' is therefore not allowed access.
The response had HTTP status code 403.
The first call to login goes through. The second call never hits the web server.
Not sure why. Maybe I need to tell it to remember the Origin?

Try adding proxy URLs:
From Handling CORS issues in Ionic:
Set up your ionic.project file to be something like:
{
"name": "proxy-example",
"app_id": "",
"proxies": [
{
"path": "/api",
"proxyUrl": "http://cors.api.com/api"
}
]
}

You have to add following in your web services header:
Access-Control-Allow-Origin: http://localhost:8100
Also, You can use ionic native http plugin to resolve cors issue.
I have used this in our projects successfully to resolve cors issue.
Here is the link of this plugin: https://ionicframework.com/docs/native/http/

Related

Http 503 for some Rest api call

I am running an SpringBoot application and I have a controller which defines Rest Api and accepts a request by a list of ids. Normally it works fine but if number of ids gets bigger, then I receive http 503. Interesting thing is that I cannot see any logs that my request was received by controller as well. Therefore I am not able to understand what is happening. Do you have any idea what can be worth to check ?
503 is a error code, which means service or url or controller, that you are requested is not running/available, make sure your jar file is running and also make sure the url you requested is valid. since service is not available you cant see any logs or error messages. because your controller dint receive any request yet.
The 503 error indicate that the server is not ready to handle the request.
There are some limitations for transferring data through URL , basically the length of the URL
In general the limit was 2047 chars and it is different for different browsers
503 means Service Unavailable.
I think something is wrong with your request.
You can try to add a custom OncePerRequestFilter and log your HTTP Request.

JBPM : POST request for execute process go wrong

I am new to jBPM.
I reference jbpm.org for tutorial. And I tried "My First Business Application" example. It is OK.
And then I tried the next tutorial "Business Application with Business Asset". The first steps are going well. I successfully done installation of jBPM Console, drawing business process asset in KIE process and starting the application are going well.
And I tested according to reference. I got a problem for sending POST request to my application. I use Chrome Postman client for sending POST request.
In tutorial, they give the following link to make POST request.
http://localhost:8090/rest/server/containers/business-application-kjar/processes/business-application-kjar.process/instances
I do but I always get "415 Unsupported Media Type" in every time. I also research for error. I can't find anywhere.
Please help me.
I am trouble for continue the next examples.
I want to know is any way to continue or am I wrong in sending request.
Thanks.
415 - Unsupported Media Type error occurs when the request is in a format not supported by the server.
Add the following http header parameters to your request.
Accept: application/json
Content-type: application/json
X-KIE-ContentType: JSON

IntelliJ Static Web Project to Tomcat or Angular CORs

I have a static web angular project in IntelliJ IDEA. The static page gets deployed to http://localhost:63342/Calculator/app/index.html. I have run into a problem where I try to post some data to a server to get a response back but when I try to post I get this error:
XMLHttpRequest cannot load <url>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 401.
Here is my post angular code:
WebIdServer.prototype.getId = function(id) {
var _this = this;
var request = {
method: 'POST',
url: 'https://<url>,
headers: {
'Authorization':'Bearer QWE234J234JNSDFMNNKWENSN2M3',
'Content-Type':'application/json',
},
data: {
id:id
}
};
_this.$log.debug(request);
return _this.$http(request)
.success(function(data, status, headers, config){
_this.$log.debug("Successfull request.");
/*called for result & error because 200 status*/
_this.uid = data.id;
_this.$log.debug(_this.uid);
})
.error(function(data, status, headers, config){
_this.$log.debug("Something went wrong with the request.");
_this.$log.debug(data);
/*handle non 200 statuses*/
});
};
I know for a fact that post works because I tried it on a local url of my application that I had running on a different port.
So my question is, since I can't post from localhost I was wondering if maybe deploying this to a tomcat server would fix things. If so, how do you deploy this static web project to a tomcat server? If that's not necessary, then how do I get around this problem I'm having?
There's a few things regarding CORS. It's a web browser telling you you cannot make a particular call. This is only a front end problem, a script running on a server can call any api regardless of the location. Three different options:
without config; same hosts
Without any configuration on your server, your front end's AJAX requests need to match both the domain and the port of the service you're calling. In your case, your angular app at http://localhost:63342 should be calling a service also hosted on http://localhost:63342 and then you're sweet. No CORS issues.
with server side config; different hosts
If the API is hosted elsewhere, you'll have to configure the API host. Most servers will let you configure access controls, to allow a particular domain to bypass the CORS block. If you have access to the server you're trying to call, see if you can set it up. The enable CORS website has examples for most servers. Usually this is pretty simple.
Create a proxy
This is your Tomcat idea. CORS is only a problem if your front end calls another service. A server script can call anything it likes. So, you could use Tomcat (or Apache, or NGINX, or NodeJS...) to host a script that'll pass on the request. Essentially, all it needs to do is add Access-Control-Allow-Origin: * to the response of the API.
I have never used Tomcat myself, but here's a blog post that might have some useful info on how to do that. Combine it with the info on enable CORS and you should be able to route anything to anywhere.
This process is common. Just look at the popularity of a node package like CORS anywhere, which is what your tomcat does.
As a disclaimer, how good of an idea this is depends on how you can pass along the credentials and tokens. You don't really want to create a service that'll just blindly call someone else's API with your credentials.

Cross Domain Error with self built API

The server sending JSON to the API is a Tomcat server in the Gradle packages (it is built in Java).
I am having trouble's making an API call with Angular. I know my API is working because I can view it on "Postman."
var app = angular.module("todo", []);
app.controller("AppCtrl", function($http){
$http.get("192.168.5.100:8080/aggregators/datafile")
.success(function(data){
console.log(data)
})
})
When I run it I get the following error:
XMLHttpRequest cannot load %3192.168.5.100:8080/aggregators/datafile. Cross origin requests are only supported for HTTP.
The problem you're running into is that you can't make cross origin requests from the browser without CORS or using JSONP.
Postman operates outside of the context of the browser (as if you had issued a cURL request, if you're familiar with cURL).
This is for security reasons.
So, how do you implement JSONP? It really depends on the server, but in general, your resource would look for a GET request that had a pre-determined querystring parameter (normally callback for simplicity):
http://192.168.5.100:8080/aggregators/datafile?callback=mycallback
How do you make a JSONP call?
The server wraps the JSON in that callback, causing it to look something like the following:
mycallback({json:object});
This Stack Overflow answer goes into more detail.
The callback is the function the browser should hit when the request is executed, and that's what allows for cross-domain requests.
Now, on to CORS.
CORS is a system for allowing the browser to communicate with the server to determine whether or not it should accept a cross domain request. It's a bit complicated, but in general it involves settings up certain Headers on your API Server; and then executing an Ajax request in a particular fashion (for JQuery, use the withCredentials property for $.ajax). The server checks where the request is from, and if it's a valid source, it let's the browser know and the browser allows the request (I'm being simplistic).
MDN has a thorough explanation of CORS that is worth reading.

AppEngine processing my POST requests as GET

I encountered a weird stuff and I've been struggling with it for a while now. Google AppEngine is processing my POST requests as GET. I've tried 3 different REST clients and they produced the same error (an html document that said GET is not available for the specified URL; but for God's sake I sent a POST!).
I'm using Java, and I've set the web.xml file correctly. Please note that this is not the first servlet in the project that responds to POST and others have worked properly. To complicate matters, this same servlet responds to POST normally when I run the app on my local development appengine server. I've cleaned and rebuilt the project several times and even changed the URL of the servlet but the problem still persists. Any help? Thanks.
The most common reason for this is that your POST is redirected for some reason, and redirects are always GET. What does the logging console say?
I was using the non secure url and it redirects POST to GET. Using https fixed this.

Categories

Resources