Hi All!
A colleague and I are investigating the use of SAS Viya's API's to call a model (we are both SAS programmers and have very little experience with API's), I feel like we have made some good progress but seem to be falling at the last hurdle, submitting data to the model.
we have a dataset which we want to submit to the model and are using proc json to convert it and then proc http to submit it, ensuring only the variables that model is expecting exist on the final dataset by using the modules endpoint but whatever we do we seem to end up with a "415 Unsupported Media Type" error... if someone can help us out it would be massively appreciated:
/* Setup steps */
*/ Obtain URL for the environment;
%let myviyaurl=%sysfunc(getoption(servicesbaseurl));
/* define global macros */
%global status;
%global myurl;
/* define files used */
filename resp temp; /*temporary response handler*/
filename hdrin temp; /*temporary header handler for sending*/
filename headers temp;/*temporary header handler for recieving*/
/* define macros */
/*macro for building URL's to use in API calls*/
%macro buildURI(suffix);
data _null_;
url=cats("'", "&myviyaurl", "&suffix.","'");
call symputx('myurl', url, 'G');
run;
%mend buildURI;
/*Macro to make API calls*/
%macro make_API_call(type=GET,Headers=);
proc http
method="&type."
url=%unquote(%superq(myURL)) out=resp
OAUTH_BEARER=SAS_SERVICES VERBOSE;
%if &Headers ne %then %do;
headers
&headers.
;
%end;
run;
%mend make_API_call;
/*MAIN PROCESS FLOWS*/
*/ Build URL for the REST API call with correct endpoint;
%buildURI(/microanalyticScore/modules);
*/Run REST API call to get list of discorvery agents using PROC HTTP;
%make_API_call(type=GET);
*/ Define a JSON library to read the API response;
libname resp json;
/* get the ordinal link for the smote model from items */
proc sql noprint;
select ordinal_items into:ordinal
from resp.Items
where id = "smote champion model";
quit;
/*GET the endpoint to point to steps for next API call*/
proc sql noprint;
select URI into:suffix
from resp.Items_links
where rel = "steps" and ordinal_items = &ordinal.;
quit;
filename resp clear; /*clear response temp file*/
libname resp clear; /*clear response temp libname*/
filename resp temp; /*temporary response handler*/
*/ Build URL for the REST API call with correct endpoint;
%buildURI(&suffix.);
*/Run REST API call to get list of discorvery agents using PROC HTTP;
%make_API_call(type=GET);
*/ Define a JSON library to read the API response;
libname resp json;
/*get a list of variables that the model is expecting*/
proc sql noprint;
select name
into :var_list separated by ' '
from RESP.ITEMS_INPUTS
;quit;
/*TESTING*/
/*TESTING*/
/*TESTING*/
cas; caslib _ALL_ assign;
filename myjson clear; /*clear temporary files*/
filename resp3 clear;
libname myjson clear;/*clear temporary files*/
/*create a temporary Json file that will be used to send data to MAS*/
filename myjson "/ald/Workspace/Stewart.Jardine/Temp_JSON/smote.json"; /* Define the output JSON file */
proc json out=myjson pretty keys;
export public.SMOTE_SOURCE_DATA(keep= &var_list.)/ keys; /* Convert the dataset */
run;
filename resp3 temp;
%let Link= https://aldermore.ondemand.sas.com/microanalyticScore/modules/smote%20champion%20model/steps/score;
proc http
url= "&Link."
method="POST"
oauth_bearer=SAS_SERVICES VERBOSE
in=myjson
out=resp3;
run;
Hey Joe, thanks so much for the follow up, I did try out your recommendation but unfortunately it did not make a difference to the error message... the good news is that we found an entirely unrelated way of calling the model! I am certain i will return to using API's and JSON again in the future but for now I need to move on to another task.
thanks again for the suggestion and follow up though!
Hi @stewart_Jardine. Based on the error message, the first thing that jumps out is no headers being defined in you proc http statement at the end. The API doc on developer.sas.com has the following headers defined (you can ignore the Authorization header).
proc http
url= "&Link."
method="POST"
oauth_bearer=SAS_SERVICES VERBOSE
in=myjson
out=resp3;
headers "Accept"="application/json, application/vnd.sas.microanalytic.module.step.output+json, application/vnd.sas.error+json"
"Content-Type"="application/json";
run;
Join us for SAS Community Trivia
SAS Bowl XLIX, SAS Innovate 2025
Wednesday, February 19, 2024, at 10:00 a.m. ET | #SASBowl
Hi @stewart_Jardine. I wanted to check in and see if you've had a chance to view/consider my earlier comment? Thanks!
Join us for SAS Community Trivia
SAS Bowl XLIX, SAS Innovate 2025
Wednesday, February 19, 2024, at 10:00 a.m. ET | #SASBowl
Hey Joe, thanks so much for the follow up, I did try out your recommendation but unfortunately it did not make a difference to the error message... the good news is that we found an entirely unrelated way of calling the model! I am certain i will return to using API's and JSON again in the future but for now I need to move on to another task.
thanks again for the suggestion and follow up though!
Feel free to reach back out when you get back to this. We'll figure out exactly where the issue is.
Join us for SAS Community Trivia
SAS Bowl XLIX, SAS Innovate 2025
Wednesday, February 19, 2024, at 10:00 a.m. ET | #SASBowl
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.