SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

All, 
    Looking for an example or help on how to make a PROC HTTP request by passing a JSON message. The R code that I put together which works is below :

 

 

RawResponse3 <-POST(RequestURL,
		    add_headers('Authorization' = paste0('Bearer ',AccessToken)),
		    add_headers('Content-Type' = 'application/json;charset=UTF-8'),
		    body = BodyMsg, 
		    encode ='json'
		   )

  I am thinking the equivalent in SAS would be something like: 

Proc HTTP; 
  	    URL =  "&URLDes."
            Method = "post"
            ct = "application/x-www-form-urlencoded";
            headers "Authorization" = "Bearer &AccessToken.";
  	    encode = 'json';  * Not sure of this line; 
Run; 

   Now, how do I pass the JSON message which is represented by "BodyMsg" in my R code ? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

To POST some JSON content to a web service, you need to place the content in an external file, then reference that file with the IN= argument on PROC HTTP.

 

/* Put the body of the JSON content in external file */
filename json_in temp;
data _null_;
file json_in;
input;
put _infile_;
datalines;
{ "name":"value" }
run;

/* reference that file as IN= parm in PROC HTTP POST */
filename resp temp;
proc http 
 method="POST"
 url="http://httpbin.org"
 ct="application/json"
 in=json_in
 out=resp; 
run;
Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!

View solution in original post

8 REPLIES 8
ChrisHemedinger
Community Manager

To POST some JSON content to a web service, you need to place the content in an external file, then reference that file with the IN= argument on PROC HTTP.

 

/* Put the body of the JSON content in external file */
filename json_in temp;
data _null_;
file json_in;
input;
put _infile_;
datalines;
{ "name":"value" }
run;

/* reference that file as IN= parm in PROC HTTP POST */
filename resp temp;
proc http 
 method="POST"
 url="http://httpbin.org"
 ct="application/json"
 in=json_in
 out=resp; 
run;
Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!
UdayGuntupalli
Quartz | Level 8

@ChrisHemedinger,
         The solution you have offered works. But it looks like this cannot be used within a macro. Would you have an alternative if this needed to be implemented in a macro ? 

ChrisHemedinger
Community Manager

As you discovered, DATALINES (or CARDS or similar) doesn't work inside of a macro.  If the JSON data that you're generating requires the dynamic processing of a macro construct, consider using PUT statements or another data-driven approach like CALL EXECUTE.

Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!
niejung
Obsidian | Level 7
I am doing similar post request and got 503 error code service unavailable
What is the possible reason doe that error?
Need a little help for debug by using 9.4m3 what options do I have?
ChrisHemedinger
Community Manager

@niejung HTTP 503 is "Service Unavailable" -- which suggests the API you're calling is not available, or the URL you're using is incorrect.  I suggest that you try the call in Postman or cURL -- removing SAS from the equation -- until you can verify the call is working.

Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!
quixotik
Calcite | Level 5

FWIW, I just had to solve the same problem and did it by masking the json body with the nrbquote function and passing that to the IN parameter of the proc http, like so (where &pi_request_body contains a valid json object):

 

  %let body = %nrbquote(&pi_request_body.);

  proc http
    url="&server./piwebapi/batch"
    method="POST"
    ct="application/json"
    in="&body."
    out=&pi_response_file.;
    headers "Authorization"="Bearer &pi_token.";
  run;

HTH, Robin Barendregt

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 15070 views
  • 5 likes
  • 5 in conversation