Manipulating js rest resource responses


(Oleg Voronin) #1

Can I force a non-200 response from these resources' js code?

 

We use js rest resources for accessing certain external (to Squiz) content. Consider an example page that would take ?id=123 GET, and the js rest resource will make a request to the third-party endpoint with that value (just using global keywords). The service replies with relevant content which is then displayed.

 

I want to be able to 404 the pages asking for missing content (or making otherwise invalid requests ?id=potato). It's trivial to get this information from the endpoint response and render "not found" text, but I want to be able to modify the http response status header of the page, which plugs into our indexing setup.


(Bart Banda) #2

Hmm, I don't think so. The quickest solution I can think of is to redirect the user to a 404 page via the JS Rest asset if the response header from the Rest call is 404. 


(Tbaatar) #3

Hi Bart,

Do you have syntax example of how to write a redirect in the JS Rest response body.

I have tried the following and it doesn’t work:

 if (data.error == 'Not Found') {
   window.location.href = "https://www.example.com";
} else {
    print(_REST.response.body);
}

(Tbaatar) #4

Found the solution. It just needed to inside the script tag and printed.
print(<script>window.location.href = "https://www.example.com";)</script>;