Your SAS programs, embedded in web apps and elsewhere

creating json rest API

Reply
Frequent Contributor
Posts: 127

creating json rest API

[ Edited ]

Hello,

 

I would like to expose some data one the web through a json rest api.

I have no idea how it could be done. (I have experience building Stored processes with SAS EG (EG 7.1 & SAS 9.3) but that's about it)

 

Is it as simple as making a STP like this?

 

 

data _null_;
     file _webout;
     put '{ "name":"John", "age":31, "city":"New York" }';  
     run;

 

I tried that and when going to the url of the STP in a browser I see the json data.

 

But when retrieving the data with javascript from a web page on another server I get a cross-origin CORS error.

XMLHttpRequest cannot load https://xxx.xxx.xxx/SASStoredProcess/guest?_program=/yyy/yyy/yyy. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://xxx.xxx.xxx' is therefore not allowed access.

 

 

$.ajax({
	dataType: "json",
	url: "//xxx.xxx.xxx/SASStoredProcess/guest?_program=/yyy/yyy/yyy",
	// data: data,
	success: function(data) {
		console.log(data);
	}
});

 

 

Is it possible to add the header "Access-Control-Allow-Origin:*" in the response?

Thanks!

Frequent Contributor
Posts: 127

Re: creating json rest API

[ Edited ]

I got rid of the cross origin error by changing the response headers like this

 


%let old = %sysfunc(stpsrv_header(Content-type, application/json));
%let old = %sysfunc(stpsrv_header(Access-Control-Allow-Origin, *));

data _null_;
     file _webout;
     put '{ "name":"John", "age":31, "city":"New York" }'; 
     run;

And the ajax request seems to work as expected.

 

$.ajax({
	url: "//xxx.xxx.xxx/SASStoredProcess/guest?_program=/yyy/yyy/yyy",
	dataType: "json",
	success: function(data) {
		console.log(data.data);
		$.each( data.data, function(i, value){
			console.log(value);
		});
	},
	error: function(e,f,g) {
		console.log('error');
	}
});

 

Still have no idea if this is a good way for achieving this.

What happens when the STP gets more complicated and takes more than a fraction of a second to complete?

Is this REST?

Contributor
Posts: 39

Re: creating json rest API

Hello Mathias, 

 

This is a perfectly valid way of coding a web service yourself. You also have valid concerns. 

 

Here are a few takeway notes: 

* Your web service works becasue you have manually logged on. Try to log out of SAS Stored Process Web Application and run the request, it will not work becasue SAS will ask you to authenticate when your ajax request won't handle that request. 

* Your way of changing headers applies only to your webservice. This means you can't use the same method to log in to your webservice as SAS Logon manager will not have the correct headers. 

* Default setting for SAS Web Server is to time out after one minute. It can be changed, but at the same time, would your users wait for at least 30 seconds to refresh a datasource on the screen? If web service takes more then a few seconds to fetch the data, something needs to change. 

 

I have two solutions to suggest: 

* Consider using an open source connector form Boemska: https://github.com/Boemska/h54s This is tried and tested way of getting data out SAS, but it requires certain level of access to the SAS platform. 

* Analytium, the company that I am working for, has a solution in the pipeline that makes it easier to integrate software with SAS. It would be great if I could give you a quick demo over WebEx to see how Analytium could help you get the most out SAS. If you are interested, just let me know your timezone, availability and email by sending me a private message: Send Message to Vasilij Nevlev 

 

Regards,

Vasilij

=======================================
For more information about {An}alytium, visit https://www.analytium.co.uk
Super Contributor
Posts: 275

Re: creating json rest API

See if this helps. We ran into a CORS on a AWS server hosting the sas stored process web app. 

 

http://support.sas.com/kb/59/957.html

Ask a Question
Discussion stats
  • 3 replies
  • 182 views
  • 1 like
  • 3 in conversation