Hello everyone
I'm trying to call cURL within SAS to access an api with oauth 1.0. In order to this, I need to construct an appropriate header. Unfortunately, I cannot specify the header correctly in SAS because of the quotes. I've read several posts here and on other sites about how to handle quotes within a macro variable. My code forms a macro variable with the necessary quotes. The issue is when I put it in the filename statement. I get an error.
%let consumer_key=%quote(")cheezwhiz%quote(");
%let header = "curl --header 'Authorization: OAuth oauth_consumer consumer_key=&consumer_key.'";
%put &header; gives "curl --header 'Authorization: OAuth oauth_consumer consumer_key="cheezwhiz"'" which is exactly what I want.
However, when I try to execute this within a filename statement, e.g., filename curl pipe &header.;, I get the following error message: ERROR 23-2: Invalid option name cheezwhiz.
Does anyone know of a way to circumvent this error?
Thanks very much for any suggestions.
-Bill
Try letting the quote function have all the fun.
Try letting the quote function have all the fun.
data_null_,
Thanks so much for the code! The quote function is definitely having all the fun now.
Thanks again,
Bill
Quick follow-up question: How do I add additional quoted strings to the pipe statement. For example, I tried to add a consumer_secret key with code below.
%let consumer_key=cheezwhiz;
%let consumer_secret=coorslight;
%let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer consumer_key=%sysfunc(quote(&consumer_key)),consumer_secret=%sysfunc(quote(&consumer_secret))))));
%put NOTE: %superq(header);
filename curl pipe &header.;
filename curl list;
The code runs, but I cannot see the second key (consumer_secret) in the log. What's the best way to deal with this issue? Should I use the catx function? Or is there a more direct solution?
-Bill
It's the comma. Do you need it?
Thanks for the response and yes I need the commas. They're required for the OAuth header.
Put the comma in %NRSTR(,) %STR might work too.
data _null_,
Thanks very much for the additional code!
-Bill
There are plenty of method available in SAS to execute these http calls without having to use an external tool, such as cURL.
%let consumer_key=cheezwhiz;
%let consumer_secret=coorslight;
filename headIn temp;
data _null_;
file headIn;
array oauth[2] $ 200 consumer_key consumer_secret;
put 'Authorization: OAuth oauth_consumer ';
do _n_=1 to dim(oauth);
oauth[_n_]=quote(symget(vname(oauth[_n_])));
if _n_>1 then put +(-1) ',';
put +5 oauth[_n_]= @;
end;
run;
filename out temp;
proc http method='post' headerin=headIn url="http://localhost:8681/OAuth/token" out=out;
With careful counting of the number of quotation marks needed, you can do what you are looking to get without using any functions.
%let consumer_key=cheezwhiz;
%let consumer_secret=coorslight;
filename cURL pipe "curl -H ""Authorization: OAuth oauth_consumer consumer_key=""""&consumer_key"""", consumer_secret=""""&consumer_secret""""""";
filename cURL list;
NOTE: Fileref= CURL
Physical Name= curl -H "Authorization: OAuth oauth_consumer consumer_key=""cheezwhiz"", consumer_secret=""coorslight"""
Side note, I've never seen an OAuth scheme like this, or that required quoting of the key and secret in the heading before, but I'll assume you know what you're doing on this front.
FriedEgg,
Thank you for your thoughts and code. I prefer cURL over proc http because I can simultaneously submit up to 3 requests at once. However, I may try proc http if I run into significant issues accessing the api via cURL.
Also, I try to carefully count the quotes to see if I can construct the header without any functions.
Thanks again,
Bill
On a side note, I have to remind you that given the current direction SAS and other IT industries are heading, proc http may live longer than curl. My company for example, has just recently decided moving to server SAS (Grid) and eradicating PC SAS completely in a short period. Many legacy SAS code, especially those involving OS command and third party utilities (such as curl) have to be modified.
Haikuo
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.