I've come across an annoying issue, which I think is being caused by IIS 8.5. With a web app that I'm creating, I have created a custom JSON HttpStatusResult class (which was an idea from this stackoverflow post), which enables me to return JSON along side a 500, 400 or other types of HTTP response status codes. Here is that code:
public class JsonHttpStatusResult : JsonResult
{
private readonly HttpStatusCode _httpStatus;
public JsonHttpStatusResult(object data, HttpStatusCode httpStatus)
{
Data = data;
_httpStatus = httpStatus;
}
public override void ExecuteResult(ControllerContext context)
{
context.RequestContext.HttpContext.Response.StatusCode = (int)_httpStatus;
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
base.ExecuteResult(context);
}
}
While testing locally, using IISExpress, this works as I'd expect - if I return a 500 Http status code along with some JSON, the response contains just the JSON. However, when I publish the site to the IIS 8.5 web server that we have (We do not yet have any running IIS 10, unfortunately - I don't think that would make a difference anyway?) it instead returns the default 500 error response page, and not the JSON:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>500 - Internal server error.</h2>
<h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</fieldset></div>
</div>
</body>
</html>
So this is what I'm stuck at - how do I get IIS to return my custom 500 JSON error result, instead of this default 500 page? I've taken a look around and seen some posts mentioning that these are two important lines of code that are needed:
HttpContext.Response.Clear();
HttpContext.Response.TrySkipIisCustomErrors = true;
However, I don't know how to set these when sending my custom error response, as once I return a JSON error response, like in this example:
return new JsonHttpStatusResult(new
{
ResponseMessage = ex.Message
},
HttpStatusCode.InternalServerError);
I'm not then able to set the response as no more code runs once the return is called? Is there some kind of way I can hook into the response once my custom Json result class has created the response, I saw something kind of like that with this post, but unfortunately it doesn't seem to explain how I'm supposed to use this?
It's worth mentioning, that the reason I'm trying to return JSON error responses is because these actions are being called by Ajax from the front end, and then these errors are handled/displayed by the javascript on the frontend, which I why I'm trying to pass back more than just a "500 internal server error" message.
Ok then, found a solution to my problem, all I had to do in my case was add this to the web.config within the system.webServer element:
<httpErrors existingResponse="PassThrough"/>
Didn't need to change any other settings, or modify the response in the end. It seems that this setting causes IIS to pass the existing response through on an error.
Related
I have an event-driven cloud function (written in Node.js) that creates video collection in Firestore whenever there are changes in Storage bucket. But when I tried to access the same data from a HTTP function, the API throws a 401 unauthorised error.
HTTP Function:
exports.getVideos = (req, res) => {
db.collection('video').get().then(querySnapshot => {
return res.status(200).json({
videos: querySnapshot.docs.map(doc => doc.data()),
total: querySnapshot.size
});
})
.catch(error => {
functions.logger.error('Error getting video details: ', error);
return res.status(500).json({ error });
});
};
API Response: 401 Unauthorized
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>401 Unauthorized</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Unauthorized</h1>
<h2>Your client does not have permission to the requested URL <code>/video/get-videos</code>.</h2>
<h2></h2>
</body>
</html>
I don't see any issues on the code side, so I was wondering what's the root cause of this.
It might be that different cloud functions are executed using different service accounts. And one of them has required IAM roles/permissions, and another - not.
Another option - can you check that all of that happens in one GCP project and both cloud functions are supposed to work with exactly one (and the same) firestore collection?
I just started with Angular and I have an issue with its components and an API call. I have the following content in these files
user.component.html:
<div>
<p>
{{user.name}}
</p>
</div>
user.component.ts:
getUser() {
this.user = this.postService.getPosts();
console.log('TWO');
console.log(this.user);
return false;
}
user.service.ts:
getPosts(){
this.http.get('https://XXXXX/yyy').subscribe(data => {
this.str = data[0];
console.log('ONE');
});
return this.str;
}
The Get response is done correctly and I am able to display the data in the html file ONLY if user is loaded, but the first time that I try to do the request to the API the component.ts finishes its execution before the service, and user is not populated correctly, making the html render empty, so with above code I get this printed in console:
TWO
undefined
ONE
I think if it would be synchronized, it would print:
ONE
John
Two
I found this post but I wasn't able to understand it: Angular - Wait until I receive data before loading template
Can someone please try to help me with this?
I have such url - http://www.coolsite.com/daily-plan/#id=1
What the easiest way to parse that string and read a hash value (the value after #id=)?
Thank you
On client side (i.e. from JavaScript) you can check window.location.hash to get hash. On server side, general answer is 'it is impossible' since hash is not sent in request to server.
Upd: I maybe misunderstood the question. My answer is about how to get hash part of url either in browser or in server side code during request processing, not about string processing.
Upd2: Answer to comment here because it doesn't fit in comment.
How does it work when user clicks on your navigational links?
I assume hash is changed and corresponding content is downloaded via AJAX request from web service or REST.
For example if your user has URL www.example.com in his browser and this page shows a list of product categories. User clicks one category and URL changes to www.example.com/#id=5 and products from that category(with ID=5) are downloaded via AJAX and shown on the page. No postback, only partial page refresh.
Is this close to your scenario?
Now you want user to paste/enter www.example.com/#id=5 directly in the browser address bar and go directly to list of products in that category.
But /#id=5 is not sent to server with request by the browser, so there is no way to get that value on server side, and you can do nothing about it since it is the browser decided not to send this data and you don't have it on server side.
In our project we use solution when server returns only common page code/html, i.e. header, footer, without main/center part of the page. Then there is a JavaScript code which executes right after this common HTML loaded. It takes window.location.hash and sends it to web service via AJAX and web service returns content (HTML) for the main part of the page.
new URI("http://.../abc#xyz").getFragment();
See the Javadocs for URI
Here is how to capture anchor links. Works across all web frameworks.
I'll use an example scenario to illustrate: let's say we need to capture a deep URL http://server.com/#/xyz requested by an unauthenticated user so that they can be redirected to that deep URL post-login.
The unauthenticated user requests http://server.com/#/xyz (everything from the '#' onwards is not sent to the server).
All the server knows is that the user wants http://server.com/ and that they are unauthenticated. Server redirects the user to a login form.
Here's the clever bit: the client is still waiting on their original request so if the server includes a hidden element in the login form with some JS that references window.location.href, it can capture the full URL of the original request complete with the anchor portion:
<form action="/login" method="post">
<div>
<label>Username:</label>
<input type="text" name="username"/><br/>
</div>
<div>
<label>Password:</label>
<input type="password" name="password"/>
</div>
<!-- XXXXXXXXX CLEVER BIT XXXXXXXXXX-->
<script>
document.write('<input type="hidden" name="from" value="'+document.location.href+'"/>');
</script>
<!-- XXXXXXXXXX-->
<div>
<input class="submit-button" type="submit" value="Submit"/>
</div>
</form>
The user authenticates themself and the original URL is sent with the POST. The server can then relay the user to the original deep URL.
String url = " http://www.coolsite.com/daily-plan/#id=1";
int sharpPos = url.indexOf('#');
String q = null;
if (sharpPos >= 0) {
q = url.substring(sharpPos);
}
Surely you can use various methods of string manipulation including regular expressions.
But actually your example is strange. Typically parameters of URL are passed after question mark. In this case you can just use standard class URL:
String q = new URL(" http://www.coolsite.com/daily-plan?id=1").getQuery();
what you are using to do this ?
If you are using jsp or servlet following will be useful to you
if (request.getParameter("#id") == null) {
out.println("Please enter your name.");
} else {
out.println("Hello <b>"+request.getParameter(i)+"</b>!");
}
If you are using javascript for it following function will be useful to you
function getURLParameters()
{
var sURL = window.document.URL.toString();
if (sURL.indexOf("?") > 0)
{
var arrParams = sURL.split("?");
var arrURLParams = arrParams[1].split("&");
var arrParamNames = new Array(arrURLParams.length);
var arrParamValues = new Array(arrURLParams.length);
var i = 0;
for (i=0;i<arrURLParams.length;i++)
{
var sParam = arrURLParams[i].split("=");
arrParamNames[i] = sParam[0];
if (sParam[1] != "")
arrParamValues[i] = unescape(sParam[1]);
else
arrParamValues[i] = "No Value";
}
for (i=0;i<arrURLParams.length;i++)
{
alert(arrParamNames[i]+" = "+ arrParamValues[i]);
}
}
else
{
alert("No parameters.");
}
}
REPLACE the '#' with '?' when parsing the url. Check the code below
String url = "http://www.coolsite.com/daily-plan/#id=1";
String urlNew = url.replace("#", "?");
String id = Uri.parse(urlNew).getQueryParameter("id");
If you URL will the same as you write and doesn't contains anythins else then whis code on Java will help you
String val = "http://www.coolsite.com/daily-plan/#id=1";
System.out.println(val.split("#id")[1]);
Don't forget check to null value.
P.S. If you use servlet you can get this parameter from request.getAttribute("id").
With best regards,
Psycho
if your url get from OAuth callback,then you can't!
because the full url won't send to service because of hash(#)
I'm trying to start SuperDevMode the following way. I modified my index.jsp page to append a configurable prefix to the .nocache.js file. Production config contains an empty prefix, while development config contains something like http://localhost:9876. Then I start the server on Tomcat and type localhost:8080. GWT starts normally, but when it tries to send an RPC request, it fails, because request goes to localhost:9876 instead of localhost:8080. How can I customize which host should RPC use to send request to?
I spent some time digging into GWT code with Eclipse and debugger, and found, that GWT RPC concatenates the result of GWT.getModuleBaseURL() with the value of #RemoteServiceRelativePath. Here is the code of the getModuleBaseURL method:
public static native String getModuleBaseURL() /*-{
// Check to see if DevModeRedirectHook has set an alternate value.
// The key should match DevModeRedirectHook.js.
var key = "__gwtDevModeHook:" + $moduleName + ":moduleBase";
var global = $wnd || self;
return global[key] || $moduleBase;
}-*/;
I also examined dev_mode_on.js and found several occurences of __gwtDevModeHook, but none of them contained moduleBase. Also dev_mode_on.js install all its hooks into sessionStorage, while getModuleBaseURL reads from $wnd (I used debugger and became convinced that $wnd = window).
So I used the following solution and it worked for me. I simply added the following into my index.jsp:
<c:if test="${not empty gwtScriptPrefix}">
<script type="text/javascript">
window["__gwtDevModeHook:MYAPP:moduleBase"] = "MYAPP/";
</script>
</c:if>
just before
<script type="text/javascript"
src="${gwtScriptPrefix}MYAPP/MYAPP.nocache.js">
</script>
where the gwtScriptPrefix attribute is calculated by System.getProperty, and the corresponding system property is set to localhost:9876/ on the Eclipse running configuration.
I am very new to angularJS. I am searching for accessing services from RESTful API, but I didn't get any idea. How can I do that?
Option 1: $http service
AngularJS provides the $http service that does exactly what you want: Sending AJAX requests to web services and receiving data from them, using JSON (which is perfectly for talking to REST services).
To give an example (taken from the AngularJS documentation and slightly adapted):
$http({ method: 'GET', url: '/foo' }).
success(function (data, status, headers, config) {
// ...
}).
error(function (data, status, headers, config) {
// ...
});
Option 2: $resource service
Please note that there is also another service in AngularJS, the $resource service which provides access to REST services in a more high-level fashion (example again taken from AngularJS documentation):
var Users = $resource('/user/:userId', { userId: '#id' });
var user = Users.get({ userId: 123 }, function () {
user.abc = true;
user.$save();
});
Option 3: Restangular
Moreover, there are also third-party solutions, such as Restangular. See its documentation on how to use it. Basically, it's way more declarative and abstracts more of the details away from you.
The $http service can be used for general purpose AJAX. If you have a proper RESTful API, you should take a look at ngResource.
You might also take a look at Restangular, which is a third party library to handle REST APIs easy.
Welcome to the wonderful world of Angular !!
I am very new to angularJS. I am searching for accessing services from RESTful API but I didn't get any idea. please help me to do that. Thank you
There are two (very big) hurdles to writing your first Angular scripts, if you're currently using 'GET' services.
First, your services must implement the "Access-Control-Allow-Origin" property, otherwise the services will work a treat when called from, say, a web browser, but fail miserably when called from Angular.
So, you'll need to add a few lines to your web.config file:
<configuration>
...
<system.webServer>
<httpErrors errorMode="Detailed"/>
<validation validateIntegratedModeConfiguration="false"/>
<!-- We need the following 6 lines, to let AngularJS call our REST web services -->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
</system.webServer>
...
</configuration>
Next, you need to add a little bit of code to your HTML file, to force Angular to call 'GET' web services:
// Make sure AngularJS calls our WCF Service as a "GET", rather than as an "OPTION"
var myApp = angular.module('myApp', []);
myApp.config(['$httpProvider', function ($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
Once you have these fixes in place, actually calling a RESTful API is really straightforward.
function YourAngularController($scope, $http)
{
$http.get('http://www.iNorthwind.com/Service1.svc/getAllCustomers')
.success(function (data) {
//
// Do something with the data !
//
});
}
You can find a really clear walkthrough of these steps on this webpage:
Using Angular, with JSON data
Good luck !
Mike
Just to expand on $http (shortcut methods) here: http://docs.angularjs.org/api/ng.$http
//Snippet from the page
$http.get('/someUrl').success(successCallback);
$http.post('/someUrl', data).success(successCallback);
//available shortcut methods
$http.get
$http.head
$http.post
$http.put
$http.delete
$http.jsonp
For instance your json looks like this :
{"id":1,"content":"Hello, World!"}
You can access this thru angularjs like so:
angular.module('app', [])
.controller('myApp', function($scope, $http) {
$http.get('http://yourapp/api').
then(function(response) {
$scope.datafromapi = response.data;
});
});
Then on your html you would do it like this:
<!doctype html>
<html ng-app="myApp">
<head>
<title>Hello AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="hello.js"></script>
</head>
<body>
<div ng-controller="myApp">
<p>The ID is {{datafromapi.id}}</p>
<p>The content is {{datafromapi.content}}</p>
</div>
</body>
</html>
This calls the CDN for angularjs in case you don't want to download them.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="hello.js"></script>
Hope this helps.