- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Have you seen How to test PROC HTTP and the JSON library engine posted by @ChrisHemedinger?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What is the possible reason doe that error?
Need a little help for debug by using 9.4m3 what options do I have?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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