BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Croftie
Calcite | Level 5

I have been reading post on conversion of CURL syntax to PROC HTTP for  a while now and cannot crack this one.

I am using Base SAS 9.4 TS Level 1M7 on the X64_10PRO platform. (latest version).

The follow curl statement works fine:

x(curl -i -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "X-Accellion-Version: 16" --header "Authorization: Bearer &token." -d
"{\"notify\": true,\"emails\": [\"croftie@tpg.com.au\"],\"roleId\": 7,\"notifyFileAdded\": false}" "https://pluto.acara.edu.au/rest/folders/100/members");

 

I've tried mimicking a lot of the code snippets on this site, but as the following attempt suggest, nothing works yet.

 

/* Attempt 1. /

 

filename json_in temp;
data _null_;
file json_in;
put '{';
put '"notify": true,';
put '"emails": [';
put ' "croftie@tpg.com.au"';
put ' ],';
put ' "roleId": 7,';
put ' "notifyFileAdded": false';
put '}';
run;

proc http 
method="POST"
url="https://pluto.acara.edu.au/rest/folders/100/members"
ct="application/json"
in="S:\My School QA\2021 Data Collection\SBD_Independents\FTP_Account_Creation\Requests\test1.json"
out=resp; 
headers "Authorization"="Bearer &token.";
run;

/* NOTE: 400 Bad Request */

/* Attempt 2. /

proc http
url="https://pluto.acara.edu.au/rest/folders/100/members"
method=POST
in = FORM ("notify"="true"
"emails"="[croftie@tpg.com.au]"
"roleID"="7"
"notifyFileAdded"="false");
headers "Authorization"="Bearer &token.";
run;

/* NOTE: 422 Unprocessable Entity */

/* Attempt 3. /

proc http
url="https://pluto.acara.edu.au/rest/folders/100/members"
method=POST
in = "{'notify':'true','emails':'['croftie@tpg.com.au'],'roleId':'7','notifyFileAdded':'false'}";
headers "Authorization"="Bearer &token.";
run;

/* NOTE: 422 Unprocessable Entity */

/* Attempt 4. /

/* EXTERNAL JSON TEXT FILE CONTAINING:
{
"notify": true,
"emails": [
"croftie@tpg.com.au"
],
"roleId": 7,
"notifyFileAdded": false
}
*/

proc http
method="POST"
url="https://pluto.acara.edu.au/rest/folders/100/members"
ct="application/json"
in="S:\My School QA\2021 Data Collection\SBD_Independents\FTP_Account_Creation\Requests\test1.json"
out=resp;
headers "Authorization"="Bearer &token.";
run;

/* NOTE: 400 Bad Request */

/* Attempt 5. /

proc http
url="https://pluto.acara.edu.au/rest/folders/100/members"
method=POST
in = "notify=true,emails=[croftie@tpg.com.au],roleID=7,notifyFileAdded=false";
headers "Authorization"="Bearer &token.";
run;

/* NOTE: 422 Unprocessable Entity */

Any help would be very gratefully accepted at this stage.

Thanks in advance

Steve

1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee

Steve,

 

Stepping through your attempts, you are close each time, but have simple mistakes we can easily address:

 

Attempt 1

When you use the IN= option with a quoted string, that data is posted, literally.  So instead of sending the contents of the JSON file as you probably assume you are, you are actually just sending the web service the literal string "S:\My School QA\2021 Data Collection\SBD_Independents\FTP_Account_Creation\Requests\test1.json".  The web service is expecting JSON data (because you specified the content_type), which this is not, so it responds with "Bad Request"

 

Instead you want to use the fileref you created when you wrote the json data to a file in the first data step ie:

filename json_in temp;
...
proc http
...
    in=json_in /*no quotes*/
...

 

 

Attempt 2

Here you have a similar issue.  You are using the FORM method on the IN= option.  This helper method serializes the data you provide as a content-type such as application/x-www-form-urlencoded which your web service doesn't appear to support.  The bad content type and data is why you receive the "Unprocessable Entity" error

 

You cannot use the FORM or MULTI helpers here in the IN= option as they are not valid for the type of data you want to POST

 

Attempt 3

This time the issue is slightly different.  You are correctly trying to use the IN= option with string data equivalent to the JSON you want to post, however, you are using single quotes inside your JSON which is invalid syntax.  You also should be including the CT= option with application/json

 

Instead, you want to use double quotes inside the JSON string as you have elsewhere in your post here:

proc http
...
in="{""notify"": true, ""emails"": [""croftie@tpg.co.au""], ""roleId"": 7, ""notifyFileAdded"": false}"
...

Attempt 4

This is essentially the same as attempt 1

 

Attempt 5

This is essentially the same as attempt 3, this is invalid JSON syntax so the web service cannot handle the data it receives.

 

Additional Information

Across all of your attempts you are also leaving out additional headers that could be important to your web service successfully responding to your request.  Here is the most complete translation of your curl command in the HTTP Procedure

 

proc http
    method="POST"
    url="https://pluto.acara.edu.au/rest/folders/100/members"
    ct="application/json"
    oauth_bearer="&token" /* equivalent of headers "Authorization"="Bearer &token" */
    in="{""notify"": true, ""emails"": [""croftie@tpg.co.au""], ""roleId"": 7, ""notifyFileAdded"": false}";
    headers 
        "Accept"="application/json"
        "X-Accellion-Version"="16";
    debug level=3 NO_REQUEST_BODY NO_REQUEST_HEADERS OUTPUT_TEXT;
run;

 

View solution in original post

2 REPLIES 2
FriedEgg
SAS Employee

Steve,

 

Stepping through your attempts, you are close each time, but have simple mistakes we can easily address:

 

Attempt 1

When you use the IN= option with a quoted string, that data is posted, literally.  So instead of sending the contents of the JSON file as you probably assume you are, you are actually just sending the web service the literal string "S:\My School QA\2021 Data Collection\SBD_Independents\FTP_Account_Creation\Requests\test1.json".  The web service is expecting JSON data (because you specified the content_type), which this is not, so it responds with "Bad Request"

 

Instead you want to use the fileref you created when you wrote the json data to a file in the first data step ie:

filename json_in temp;
...
proc http
...
    in=json_in /*no quotes*/
...

 

 

Attempt 2

Here you have a similar issue.  You are using the FORM method on the IN= option.  This helper method serializes the data you provide as a content-type such as application/x-www-form-urlencoded which your web service doesn't appear to support.  The bad content type and data is why you receive the "Unprocessable Entity" error

 

You cannot use the FORM or MULTI helpers here in the IN= option as they are not valid for the type of data you want to POST

 

Attempt 3

This time the issue is slightly different.  You are correctly trying to use the IN= option with string data equivalent to the JSON you want to post, however, you are using single quotes inside your JSON which is invalid syntax.  You also should be including the CT= option with application/json

 

Instead, you want to use double quotes inside the JSON string as you have elsewhere in your post here:

proc http
...
in="{""notify"": true, ""emails"": [""croftie@tpg.co.au""], ""roleId"": 7, ""notifyFileAdded"": false}"
...

Attempt 4

This is essentially the same as attempt 1

 

Attempt 5

This is essentially the same as attempt 3, this is invalid JSON syntax so the web service cannot handle the data it receives.

 

Additional Information

Across all of your attempts you are also leaving out additional headers that could be important to your web service successfully responding to your request.  Here is the most complete translation of your curl command in the HTTP Procedure

 

proc http
    method="POST"
    url="https://pluto.acara.edu.au/rest/folders/100/members"
    ct="application/json"
    oauth_bearer="&token" /* equivalent of headers "Authorization"="Bearer &token" */
    in="{""notify"": true, ""emails"": [""croftie@tpg.co.au""], ""roleId"": 7, ""notifyFileAdded"": false}";
    headers 
        "Accept"="application/json"
        "X-Accellion-Version"="16";
    debug level=3 NO_REQUEST_BODY NO_REQUEST_HEADERS OUTPUT_TEXT;
run;

 

Croftie
Calcite | Level 5

Thank you so much for your help. I'm going to blame baldness on pulling my hair out trying to get the syntax right.

Steve

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 2 replies
  • 2058 views
  • 2 likes
  • 2 in conversation