BookmarkSubscribeRSS Feed
KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

I am attempting to retrieve a json response from an API.  I am able to get a valid response using curl, but am unable to get PROC HTTP to work.

 

The curl statement requires a -d argument: -d '{"cpId": 1}' 

 

I presume the IN= option in the PROC HTTP statement is the way to go but I receive a Bad Request error:

 

proc http webusername="&_user" webpassword="&_pwd"
    url="&_url"
    in='payload={"cpId": 1}'
    method="POST"
    out=_cpr
    headerout=_cprh;
    headers 'Content-Type' = 'application/json';
run;

I am running 9.4M4.  Any suggestions?

6 REPLIES 6
ChrisHemedinger
Community Manager

 

With the -d option, I think the server might be expecting the input to be URL encoded, right?

 

Can you try:

 

%let pl = %sysfunc(urlencode(payload={"cpId": 1}));
proc http webusername="&_user" webpassword="&_pwd"
    url="&_url"
    in="&pl."
    method="POST"
    out=_cpr
    headerout=_cprh;
    headers 'Content-Type' = 'application/json';
run;

 

Other stabs in the dark:

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

I am still getting a 400 Bad Request error even after url encoding the IN string.

ChrisHemedinger
Community Manager

I just noticed that your curl example does not have the keyword "payload".  Is that correct?

 

If so, can you refine the attempt to:

 

%let pl = %sysfunc(urlencode({"cpId": 1}));
proc http webusername="&_user" webpassword="&_pwd"
    url="&_url"
    in="&pl."
    method="POST"
    out=_cpr
    headerout=_cprh;
    headers 'Content-Type' = 'application/json';
run;

If you can share what API you're trying to access (if it's publicly documented), we might be able to find the right approach.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

I am attempting to hit the OpenSpecimen -> Get Registrations API.  The documentation is here, but not very helpful.

 

The working curl code, with just the user and url removed is:

 

curl -H "Content-Type: application/json" -X POST -d '{"cpId": 1}' 
ChrisHemedinger
Community Manager

I can't get to that link -- hidden behind an Atlassian login.  But have you tried once without the "payload=" and not a urlendoded version of the IN=?

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

Thank You!   For posterity, here is the working code:

 

 

proc http webusername="&_user" webpassword="&_pwd"
    url="&_url"
    in='{"cpId": 1}'
    method="POST"
    out=_cpr
    headerout=_cprh;
    headers 'Content-Type' = 'application/json';
run;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 4418 views
  • 4 likes
  • 2 in conversation