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 ?
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;
Have you seen How to test PROC HTTP and the JSON library engine posted by @ChrisHemedinger?
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;
@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 ?
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...
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.
@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.
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
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!
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.