Download JSON File from API via Proxy using SAS EG

China☆狼群 提交于 2021-01-29 07:41:08

问题


I am trying to geocode adresses from within a company network using an API from the Swiss authorities. My company uses proxy servers with usernames and passwords. I am new to SAS EG and this is the code that I have so far (i had to anonymize some things in order to be allowed to post this here):

filename response temp;
options set=SSL_USE_SNI=1;
options set=SSL_SNI_HOSTNAME="api3.geo.admin.ch";
proc http
url = 'https://api3.geo.admin.ch/rest/services/api/SearchServer?searchText=Bahnhofstrasse %201%20Zürich&type=locations'
method='GET'
proxyhost = 'http://OURPROXYHOST.ch'
proxyport = 8080
proxyusername = '***'
proxypassword= '***'
out= response
ct = "application/json";
run;

The log does however not throw any errors or warnings and I am seeing no output file when running the code. If I enter the url into the browser it works.

I'm using SAS EG 7.15 HF7 (7.100.5.6177) (64-Bit). I hope you guys can help me out here.


回答1:


What do the output headers look like, user HEADEROUT= to see

Sample code (no proxy though)

filename response temp;
filename headers  temp;

proc http
  url    = "https://worldpopulationreview.com/static/states/abbr-name.json"
  method = "get"
  out    = response
  ct     = "application/json"
  headerout = headers
;

data _null_;
  infile headers;
  input; 
  put _infile_;
run;

data _null_;
  infile response obs=10;
  input;
  put _infile_;
run;

* libref name same as fileref pointing to json content;
libname response json;

proc copy in=response out=work;
run;

Log

93     headerout = headers
94   ;
95

NOTE: 200 OK
NOTE: PROCEDURE HTTP used (Total process time):
      real time           0.04 seconds
      cpu time            0.00 seconds


96   data _null_;
97     infile headers;
98     input;
99     put _infile_;
100  run;

NOTE: The infile HEADERS is:
      Filename=C:\Users\Richard\AppData\Local\Temp\SAS Temporary Files\_TD4224_HELIUM_\#LN00067,
      RECFM=V,LRECL=32767,File Size (bytes)=531,
      Last Modified=05Nov2020:09:39:53,
      Create Time=05Nov2020:09:39:53

HTTP/1.1 200 OK
Date: Thu, 05 Nov 2020 14:39:55 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
content-disposition: inline; filename="abbr-name.json"
cache-control: public, max-age=0, must-revalidate
content-length: 1057
access-control-allow-origin: *
etag: W/"672e384a87acd2f6547e5127fde8f2fe74c991498d7b468b1e439c3860554ea8"
accept-ranges: bytes
x-vercel-cache: HIT
age: 189
server: Vercel
x-vercel-id: cle1::fz9dn-1604587195926-0957f649b2c8
strict-transport-security: max-age=63072000
NOTE: 16 records were read from the infile HEADERS.
      The minimum record length was 0.
      The maximum record length was 74.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


101
102  data _null_;
103    infile response obs=10;
104    input;
105    put _infile_;
106  run;

NOTE: The infile RESPONSE is:
      Filename=C:\Users\Richard\AppData\Local\Temp\SAS Temporary Files\_TD4224_HELIUM_\#LN00066,
      RECFM=V,LRECL=32767,File Size (bytes)=1057,
      Last Modified=05Nov2020:09:39:53,
      Create Time=05Nov2020:09:39:53

{
  "AL": "Alabama",
  "AK": "Alaska",
  "AZ": "Arizona",
  "AR": "Arkansas",
  "CA": "California",
  "CO": "Colorado",
  "CT": "Connecticut",
  "DE": "Delaware",
  "DC": "District Of Columbia",
NOTE: 10 records were read from the infile RESPONSE.
      The minimum record length was 1.
      The maximum record length was 31.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


107
108  * libname same as fileref pointing to json content;
109  libname response json;
NOTE: JSON data is only read once.  To read the JSON again, reassign the JSON LIBNAME.
NOTE: Libref RESPONSE was successfully assigned as follows:
      Engine:        JSON
      Physical Name: C:\Users\Richard\AppData\Local\Temp\SAS Temporary
      Files\_TD4224_HELIUM_\#LN00066
110
111  proc copy in=response out=work;
112  run;

NOTE: Copying RESPONSE.ALLDATA to WORK.ALLDATA (memtype=DATA).
NOTE: BUFSIZE is not cloned when copying across different engines.
      System Option for BUFSIZE was used.
NOTE: There were 51 observations read from the data set RESPONSE.ALLDATA.
NOTE: The data set WORK.ALLDATA has 51 observations and 4 variables.
NOTE: Copying RESPONSE.ROOT to WORK.ROOT (memtype=DATA).
NOTE: BUFSIZE is not cloned when copying across different engines.
      System Option for BUFSIZE was used.
NOTE: There were 1 observations read from the data set RESPONSE.ROOT.
NOTE: The data set WORK.ROOT has 1 observations and 52 variables.
NOTE: PROCEDURE COPY used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds

First table copied to work (ROOT)



来源:https://stackoverflow.com/questions/64699119/download-json-file-from-api-via-proxy-using-sas-eg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!