BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BillJones
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

Try letting the quote function have all the fun.

%let consumer_key=cheezwhiz;
%let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer consumer_key=%sysfunc(quote(&consumer_key))))));
%put NOTE: %superq(header);

filename curl pipe &header.;
filename curl list;


39         %let consumer_key=cheezwhiz;
40         %let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer
40       ! consumer_key=%sysfunc(quote(&consumer_key))))));
41         %put NOTE: %superq(header);
NOTE:
"curl --header ""Authorization: OAuth oauth_consumer consumer_key=""""cheezwhiz"""""""
42        
43         filename curl pipe &header.;
44         filename curl list;
NOTE: Fileref= CURL
      Physical Name= curl --header
"Authorization: OAuth oauth_consumer consumer_key=""cheezwhiz"""

View solution in original post

10 REPLIES 10
data_null__
Jade | Level 19

Try letting the quote function have all the fun.

%let consumer_key=cheezwhiz;
%let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer consumer_key=%sysfunc(quote(&consumer_key))))));
%put NOTE: %superq(header);

filename curl pipe &header.;
filename curl list;


39         %let consumer_key=cheezwhiz;
40         %let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer
40       ! consumer_key=%sysfunc(quote(&consumer_key))))));
41         %put NOTE: %superq(header);
NOTE:
"curl --header ""Authorization: OAuth oauth_consumer consumer_key=""""cheezwhiz"""""""
42        
43         filename curl pipe &header.;
44         filename curl list;
NOTE: Fileref= CURL
      Physical Name= curl --header
"Authorization: OAuth oauth_consumer consumer_key=""cheezwhiz"""
BillJones
Calcite | Level 5

data_null_,

Thanks so much for the code!  The quote function is definitely having all the fun now. Smiley Happy

Thanks again,

Bill

BillJones
Calcite | Level 5

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

data_null__
Jade | Level 19

It's the comma.  Do you need it?

BillJones
Calcite | Level 5

Thanks for the response and yes I need the commas.  They're required for the OAuth header.

data_null__
Jade | Level 19

Put the comma in %NRSTR(,)   %STR might work too.

%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))%nrstr(,)consumer_secret=%sysfunc(quote(&consumer_secret))))));
%put NOTE: %superq(header);

filename curl pipe &header.;
filename curl list;



32         %let consumer_key=cheezwhiz;
33         %let consumer_secret=coorslight;
34         %let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer
34       ! consumer_key=%sysfunc(quote(&consumer_key))%nrstr(,)consumer_secret=%sysfunc(quote(&consumer_secret))))));
35         %put NOTE: %superq(header);
NOTE:
"curl --header ""Authorization: OAuth oauth_consumer consumer_key=""""cheezwhiz"""",consumer_secret=""""coorslight"""""""
36        
37         filename curl pipe &header.;
38         filename curl list;
NOTE: Fileref= CURL
      Physical Name= curl --header
"Authorization: OAuth oauth_consumer consumer_key=""cheezwhiz"",consumer_secret=""coorslight"""
BillJones
Calcite | Level 5

data _null_,

Thanks very much for the additional code!

-Bill

FriedEgg
SAS Employee

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.

BillJones
Calcite | Level 5

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

Haikuo
Onyx | Level 15

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1753 views
  • 6 likes
  • 4 in conversation