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;

 

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
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.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 3 replies
  • 427 views
  • 0 likes
  • 2 in conversation