Hello:
I'm trying to use PROC HTTP to access an API URL that contains double quotes, as shown:
https://example.com/api.pro?method=login&data={ "username":"testuser","password": "testpassword" }
This syntax works correctly when pasted directly into a browser. When run in SAS code as below, the call fails.
We're running SAS 9.4 (ts1m3).
Example:
proc http
url="https://example.com/api.pro?method=login&data={ "username":"testuser","password": "testpassword" }"
method = "POST"
out=resp
headerout=hdrs;
run;
The response data from the call is:
{"success":false,"error":"Web services not available at this address","errno":10000}
I'm assuming it has to do with the quotes in the URL...
Thanks in advance
Try next assignment:
url="https://example.com/api.pro?method=login&data={ ""username"":""testuser"",""password"": ""testpassword"" }"
As the url literal is written between double quotes, each inside pair "" will be treated as one ".
Double quoting causes SAS to interpret the & as a macro variable...
WARNING: Apparent symbolic reference DATA not resolved.
With response of:
{"success":false,"error":"Web services not available at this address","errno":10000}
Your browser is probably swapping in the encoded characters for the double quotes -- but PROC HTTP won't do that for you. You should probably use the URLENCODE function. See an example here. I mocked up something here that might do it.
%let values=%sysfunc(urlencode(%str({ "username":"testuser","password": "testpassword" })));
proc http
url="https://example.com/api.pro?method=login%str(&)data=&values."
method = "POST"
out=resp
headerout=hdrs;
run;
That gives me the same response:
{"success":false,"error":"Web services not available at this address","errno":10000}
The API documentation says that the info needs to be passed in a variable named DATA.
Is it possible to code that as part of an IN= clause?
Yes - you should try that.
filename resp temp;
filename hdout temp;
proc http
url="https://example.com/api.pro?method=login"
method = "POST"
in='{ "username":"testuser","password": "testpassword" }'
out=resp
headerout=hdrs;
run;
Chris:
With the in= code the API returns the following:
{"success":false,"error":"Unable to locate \"data\" field in REQUEST","errno":0}
That seems like the call can't find the data field, which is expressly identified when putting the entire call on the URL line....
Can you tell me what service you're trying to use? Sometimes it helps me to reference the doc for the API in answering these.
The "data=" is something we see on the cURL examples that are often provided in API doc, but in that case "data" is a keyword for cURL and not part of the API syntax. The body of the request -- passed in that data field -- would belong on the IN= param for PROC HTTP.
Or...do you have a cURL example that works/should work? If so, we can transcribe to PROC HTTP.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.