DATA Step, Macro, Functions and more

How do I http post method in proc ds2? Problems with sending json question.

Reply
Contributor
Posts: 44

How do I http post method in proc ds2? Problems with sending json question.

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;

Community Manager
Posts: 2,764

Re: How do I http post method in proc ds2? Problems with sending json question.

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.

Contributor
Posts: 44

Re: How do I http post method in proc ds2? Problems with sending json question.

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.

Community Manager
Posts: 2,764

Re: How do I http post method in proc ds2? Problems with sending json question.

Okay -- but note that you can more easily parse the JSON using the SAS JSON engine (SAS 9.4m4).

Contributor
Posts: 44

Re: How do I http post method in proc ds2? Problems with sending json question.

[ Edited ]

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.

Contributor
Posts: 44

Re: How do I http post method in proc ds2? Problems with sending json question.

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;

 

Community Manager
Posts: 2,764

Re: How do I http post method in proc ds2? Problems with sending json question.

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.

 

 

Contributor
Posts: 44

Re: How do I http post method in proc ds2? Problems with sending json question.

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

Community Manager
Posts: 2,764

Re: How do I http post method in proc ds2? Problems with sending json question.

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.

Contributor
Posts: 44

Re: How do I http post method in proc ds2? Problems with sending json question.

Thanks,

 

Contributor
Posts: 44

Re: How do I http post method in proc ds2? Problems with sending json question.

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.

Ask a Question
Discussion stats
  • 10 replies
  • 267 views
  • 1 like
  • 2 in conversation