BookmarkSubscribeRSS Feed
alepage
Barite | Level 11

Hello,

Here's the shell / cURL code:

curl --request POST \ --url https://yul1.qualtrics.com/API/v3/directories/directoryId/mailinglists/mailingListId/transactioncontacts \ 
--header 'Content-Type: application/json' \ 
--header 'X-API-TOKEN: token-value' \ 
--data '{ "transactionMeta": { "batchId": "BT_ZZZZ45678901234", "fields": [ "string" ] }, "contacts": [ { "firstName": "string", "lastName": "string", "email": "string", "extRef": "string", "embeddedData": { "property1": "string", "property2": "string" }, "language": "string", "unsubscribed": true, "transactionData": {} } ] }'

How do we convert this script into sas code ? Could you please provide an example where we add two new contacts and another example where we will provide a json file.

 

3 REPLIES 3
ChrisHemedinger
Community Manager

cURL is usually easy to transcribe into PROC HTTP. Looking at the elements in your cURL script:

 

filename resp temp;
proc http
 method='POST'
 url="https://yul1.qualtrics.com/API/v3/directories/directoryId/mailinglists/mailingListId/transactioncontacts"
 CT="application/json"
 in='{ "transactionMeta": { "batchId": "BT_ZZZZ45678901234", "fields": [ "string" ] }, "contacts": [ { "firstName": "string", "lastName": "string", "email": "string", "extRef": "string", "embeddedData": { "property1": "string", "property2": "string" }, "language": "string", "unsubscribed": true, "transactionData": {} } ] }'
 out=resp;
 headers "X-API-TOKEN"="your-token-value";
run;

/* Check HTTP response code */
%put &SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE;

/* print response to log */
data _null_;
 rc=jsonpp('resp','log');
run;

 

The IN= can also take a filename/fileref, so if your data is in an external JSON file, use that method.

 

The response from the API, assuming it is JSON, can be read as SAS data using the JSON engine:

libname data json fileref=resp;

 

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
alepage
Barite | Level 11

Hello, 

 

I have saw your example at the url:

https://communities.sas.com/t5/SAS-Programming/How-to-make-a-PROC-HTTP-Post-request-by-passing-a-JSO...

 

where you are using the following code which I think is easier to read.

Does the datalines portion will look like that and with or without the apostrophe?  Do we put a carriage return line feed to separate each new contact info ?

 

datalines;

{ "transactionMeta": { "batchId": "BT_ZZZZ45678901234", "fields": [ "string" ] }, "contacts": [ { "firstName": "string", "lastName": "string", "email": "string", "extRef": "string", "embeddedData": { "property1": "string", "property2": "string" }, "language": "string", "unsubscribed": true, "transactionData": {} } ] }

run;

/* Put the body of the JSON content in external file */
filename json_in temp;
data _null_;
file json_in;
input;
put _infile_;
datalines;
{ "name":"value" }
run;

 

ChrisHemedinger
Community Manager

If you use that approach (which I agree is more readable), you can put the JSON content on multiple lines, as long as the complete data remains valid JSON. Break the lines at sections like commas or braces, not in the middle of quoted values.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 888 views
  • 0 likes
  • 2 in conversation