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;
SAS Innovate 2025: Call for Content! Submit your proposals before Sept 25. Accepted presenters get amazing perks to attend the conference!

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;
SAS Innovate 2025: Call for Content! Submit your proposals before Sept 25. Accepted presenters get amazing perks to attend the conference!
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.

SAS Innovate 2025: Call for Content! Submit your proposals before Sept 25. Accepted presenters get amazing perks to attend the conference!
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.

SAS Innovate 2025: Call for Content! Submit your proposals before Sept 25. Accepted presenters get amazing perks to attend the conference!
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 2025: Call for Content

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!

Submit your idea!

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
  • 8 replies
  • 13594 views
  • 5 likes
  • 5 in conversation