BookmarkSubscribeRSS Feed
jmic_nyk
Obsidian | Level 7

Jeg har følgende CURL kommando jeg gerne vil have lavet om til PROC HTTP - og jeg er kommet noget af vejen. Det der mangler er selve transporten af (i dette tilfælde) XLSX filen.

 

CURL -X POST -F "data=@F:\SASNykredit\CURL\CURLJMICTEST2.xlsx" -F publishdate=2018-10-30 -F unpublish=2018-11-22 -F title="JMIC TEST 2018-10-30" -F category="Funktionsliste" https://ptsv2.com/t/kommunikationsplatform/post

 

Foreløbig er jeg i SAS nået til:

 

filename req temp;
filename resp temp;

 

data _null_;
file req termstr=CRLF;
put "-F 'data=@F:\SASNykredit\CURL\CURLJMICTEST2.xlsx' ";
put '-F publishdate=2018-10-30 ';
put '-F unpublish=2018-11-22 ';
put '-F title="JMIC TEST 2018-10-30" ';
put '-F category="Funktionsliste" ';
run;

 

proc http
URL="https://ptsv2.com/t/kommunikationsplatform/post"
METHOD="POST"
in=req
out=resp;
run;

 

men som nævnt så bliver XLSX filen ikke overført. Jeg har brug for at flere forskellige filformater kan overføres (pdf, xlsx, docx etc.)

 

Er der nogen der kan hjælpe og lede mig lidt på vej?

4 REPLIES 4
TorOveKilnes
SAS Employee
Hi
This may help a little bit. You need content-type and the data is formdata so it also need to be on correct form. The below code gives an http 400 error and "Error parsing multipart data", with content-type=multipart.., and http 200 with content type application/json. Unsure what is correct, as I have wrong path...
You can experiment with this. Two links that may help is also below:
** Links: https://communities.sas.com/t5/SAS-Programming/How-to-make-a-PROC-HTTP-Post-request-by-passing-a-JSO... ;;
** https://curl.haxx.se/docs/manpage.html;

** filenames, req is the body containing the form data.. ;;
** resp is the returning info from the connecting URL.. ;
filename req temp;
filename resp temp;

** Creating the request body data as pairs of form data..;;
data _null_;
file req termstr=CRLF;
put "data=@F:\SASNykredit\CURL\CURLJMICTEST2.xlsx ";
put "publishdate=2018-10-30 ";
put "unpublish=2018-11-22 ";
put "title='JMIC TEST 2018-10-30' ";
put "category='Funktionsliste' ";
run;

** The HTTP POST itself..;;
**Unclear if it is also needs a header statement for authentication. Seems not..;;
proc http
URL="https://ptsv2.com/t/kommunikationsplatform/post"
method="POST"
ct="application/json"
/* ct="multipart/form-data" */
in=req
out=resp;
run;
jmic_nyk
Obsidian | Level 7
Tnx, I'll try that
jmic_nyk
Obsidian | Level 7

The above suggestion is not quite what I needed - but on the other hand, I wasn't specific enough in my description of my needs.

 

I need the XLSX file (and other filetypes) to be posted to the URL in a binary(?) format - exactly as the cURL command does.

 

It looks like the HTTP procedure can't handle this directly, so you need some kind of "pre-burner". A datastep? 

 

I have looked at this post:

https://communities.sas.com/t5/SAS-Procedures/upload-an-excel-file-to-box-com-using-PROC-HTTP/td-p/4...

 

But it seems that the final solution has not yet come in this post. But is this the path to follow?

jmic_nyk
Obsidian | Level 7

This code works in my case. The challenge is to find out how the Content-Type and Content-Disposition statements should look like - seems like this is different between URLs and depends on the API.

 

I didn't get much help from CURL parameters (should be possible, but I was not able to figure out how), but going through (=debugging) a (rather complicated) VBA script I was able to find the correct syntax for the C-D and C-T for this URL/API.

 

Feel free to copy Smiley Happy

**********************************************************

 

filename req "F:\SASNykredit\CURL-HTTP\HTTP trial\Datafiler\request.txt";
filename resp "F:\SASNykredit\CURL_HTTP\HTTP trial\Datafiler\response.txt";
filename binfile "F:\SASNykredit\CURL-HTTP\HTTP trial\Datafiler\testdata.xlsx";

 

%let _boundary=<a valid boundary string>;
%let _cat=Testliste;
%let _publdate=%sysfunc(today(), yymmddd10.);
%let _unpubldate=%sysfunc(intnx(MONTH,%sysfunc(today(),8.),12,S),yymmddd10.);
%let _binname=%scan(%sysfunc(pathname(binfile)),-1,'\');
%let _title=%upcase(&sysuserid) TEST &_publdate.;

%put &=_boundary &=_cat &=_publdate &=_unpubldate &=_title &=_binname;

 

/*** Get the RC from PROC HTTP ***/
%macro prochttp_check_return(_exp_code);
%if %symexist(SYS_PROCHTTP_STATUS_CODE) ne 1 %then
%do;
%put ERROR: Expected &_exp_code., but a response was not received from the HTTP Procedure;
%end;
%else
%do;
%if &SYS_PROCHTTP_STATUS_CODE. ne &_exp_code. %then
%do;
%put ERROR: Expected &_exp_code., but received &SYS_PROCHTTP_STATUS_CODE.;
%put &SYS_PROCHTTP_STATUS_PHRASE.;
%end;
%else
%do;
%put NOTE: Alt i orden, forventet returkode: &SYS_PROCHTTP_STATUS_CODE. &SYS_PROCHTTP_STATUS_PHRASE.;
%end;
%end;
%mend prochttp_check_return;


/*** Set up MULTIPART-FORM data ***/
data _null_;
file req termstr=CRLF;
put "--&_boundary.";
put 'Content-Disposition: form-data; name="publish"';
put;
put "&_publdate";
put "--&_boundary.";
put 'Content-Disposition: form-data; name="unpublish"';
put;
put "&_unpubldate";
put "--&_boundary.";
put 'Content-Disposition: form-data; name="category"';
put;
put "&_cat";
put "--&_boundary.";
put 'Content-Disposition: form-data; name="title"';
put;
put "&_title";
put "--&_boundary.";
put 'Content-Disposition: form-data; name="uploadfile"; filename='"&_binname.";
put 'Content-Type: application/octet-stream';
put;
%_run(w);

 

/*** Add the binary file ***/
data _null_;
file req mod recfm=n;
infile binfile recfm=n;
input c $CHAR1.;
put c $CHAR1. @@;
%_run(w);

 

/*** Wrap it up ***/
data _null_;
file req mod termstr=CRLF;
put / "--&_boundary.--";
%_run(w);

 

/*** Calling HTTP ***/

proc http
URL="https://ptsv2.com/t/kommunikationsplatform/post"
METHOD="POST"
CT="multipart/form-data; boundary=&_boundary"
in=req
out=resp;
*DEBUG Level=1;
run;

 

/*** RCs from PROC HTTP ***/

%put &=sys_prochttp_status_code.;
%put &=sys_prochttp_status_phrase.;

 

/*** Checking the expected RC ***/
%prochttp_check_return(200);

 

/*** Finishing ***/
filename req clear;
filename resp clear;
filename binfile clear;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Discussion stats
  • 4 replies
  • 2251 views
  • 1 like
  • 2 in conversation