DATA Step, Macro, Functions and more

How to make a PROC HTTP Post request by passing a JSON message

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

How to make a PROC HTTP Post request by passing a JSON message

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 ? 

 


Accepted Solutions
Solution
a week ago
Community Manager
Posts: 3,441

Re: How to make a PROC HTTP Post request by passing a JSON message

[ Edited ]
Posted in reply to UdayGuntupalli

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;

View solution in original post


All Replies
Valued Guide
Posts: 580

Re: How to make a PROC HTTP Post request by passing a JSON message

[ Edited ]
Posted in reply to UdayGuntupalli
Solution
a week ago
Community Manager
Posts: 3,441

Re: How to make a PROC HTTP Post request by passing a JSON message

[ Edited ]
Posted in reply to UdayGuntupalli

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;
Contributor
Posts: 36

Re: How to make a PROC HTTP Post request by passing a JSON message

Posted in reply to ChrisHemedinger

@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 ? 

Contributor
Posts: 36

Re: How to make a PROC HTTP Post request by passing a JSON message

Posted in reply to UdayGuntupalli

For anyone who come down this path, you will find a solution here (https://communities.sas.com/t5/Base-SAS-Programming/datalines-and-macro-variable/m-p/474033/highligh...

Community Manager
Posts: 3,441

Re: How to make a PROC HTTP Post request by passing a JSON message

Posted in reply to UdayGuntupalli

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 116 views
  • 0 likes
  • 3 in conversation