Hello,
I am trying to use a post call with http in ds2 to send a json to an rest-server.
My problem is there is no exampel how to do, all examplel using the much easier get version. I have tried to find out on my own, but can not solve the problem. I have use the process with the standard proc http and resue the settings. Something does not work.
I have no clue-
/Anders
This is the error I got;
ERROR: Error reported by DS2 package d2http:
ERROR: Failed to determine encoding from content type.
ERROR: Error reported by DS2 package d2http:
ERROR: SETREQUESTBODYASSTRING failed for HTTP POST method with URL:
http://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy
This is my program.
proc ds2;
data test / overwrite=yes;
dcl varchar(65000) response;
method init();
dcl package http scbQuery();
dcl integer rc sc;
dcl varchar(65000) jsonQuestion;
jsonQuestion='{
"query": [
{
"code": "ContentsCode",
"selection": {
"filter": "item",
"values": [
"BE0101N1"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"2010",
"2011"
]
}
}
],
"response": {
"format": "json"
}
}';
scbQuery.createPostMethod('http://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy');
scbQuery.setRequestBodyAsString(jsonQuestion);
scbQuery.setRequestContentType('application/x-www-form-urlencoded; charset=ISO-8859-1');
scbQuery.executeMethod();
scbQuery.getResponseBodyAsString(response, rc);
sc=scbQuery.getStatusCode();
put 'RC=' rc;
put 'SC= ' sc;
put 'Response= ' response;
end;
enddata;
run;
quit;
Do you have to use DS2? You might find this easier to code and debug with PROC HTTP. I have a POST example here, using PROC HTTP to publish to a Slack channel.
I will prefere Ds2 because I need to do some loops in the final code. This because restrictions from the provider of the data. At the end, I will also use DS2 for parsing the json response.
Okay -- but note that you can more easily parse the JSON using the SAS JSON engine (SAS 9.4m4).
Shall I understand You as DS2 is not worth to use? And You recomend me, and other SAS users, not to use DS2?
I have use the last 3 weeks to developed an model in DS2 and got the most readable and easy to administrate code ever in SAS. I will not throuth it away-
On the other side, is is easier to controll that the return from HTTP is correct is much easeier with DS2 http packet. Just chheck the status code.
Here is a version with proc http and jason libname. Not so easy to sort out. The return string is direct from Statistic Sweden and work well with R.
/Ansers
Error message:
ERROR: Invalid JSON in input near line 1 column 1: Encountered an illegal character.
ERROR: Error in the LIBNAME statement.
Code:
filename svar temp ;
proc http
url="http://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy"
method="POST"
in='{
"query": [
{
"code": "ContentsCode",
"selection": {
"filter": "item",
"values": [
"BE0101N1"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"2010",
"2011"
]
}
}
],
"response": {
"format": "json"
}
}'
out=svar
ct="application/json;charset=Windows-1252";
;
run;
libname svar json fileref=svar;
proc print data=svar;
run;
Hi @AndersBergquist - no, I'm not recommending against DS2! It's a powerful language that allows for more functional control and integration with other processes like databases. I was just pointing out that while the JSON parsing package and the HTTP package are present in DS2, there are now some more elegant methods in "traditional" SAS programming. To the extent that you can mix/match these techniques in your program, it might be something to explore.
Because I think the DS2 http packets can not been use for POST, and json libname can not work with json from statistic sweden, I think the best solution would be to us proc http for the http POST and test the DS2 json for the parsing. But I think it is not posibly to mix.
The ideal solution for me, and I think lots of SAS users in Europe there the PX-wwb api is common, is a reusable package which can send and parse the to the different statistic authorities. Today, I have to make this manual several times each week. Maybe the http packages will work with POST in the future.
/Anders
I'm not sure why there is an error in the JSON engine for this result. I can replicate the problem here. I know that there have been some issues fixed with the JSON engine, including one related to Byte Order Mark (BOM) which might be the cause. The JSON response is UTF-8, I think, but the JSON engine isn't reading it properly.
I'll create a test case and send to the developer if I can't get it working.
Thanks,
Do you got any news? I recognice package HTTP och json works with variabels which limit the data I can download from Statistic Sweden. I may have to use proc HTTP and proc json for lager datasets.
Here is a quick example:
/* HTTP POST in DS2 */ proc ds2; data _null_; method init(); declare package http h(); declare varchar(1024) headers body; declare int rc status; h.createPostMethod('https://xxxxxxxxxxx'); h.setRequestContentType('application/json; charset=utf-8'); h.addRequestHeader('Accept', 'application/json'); h.addRequestHeader('VERSION', '2'); h.addRequestHeader('API-KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxx');
h.SetRequestBodyAsString('{"identifier": "xxxxx","password": "xxxxxxxxx}'); h.executeMethod(); status = h.getStatusCode(); if status = 200 then do; h.getResponseHeadersAsString(headers, rc); h.getResponseBodyAsString(body, rc ); put headers; put body; end; h.delete(); end; enddata; run; quit;
Example also can be found here:
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.