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;
It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

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;
It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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-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
  • 8 replies
  • 12567 views
  • 4 likes
  • 5 in conversation