<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Upload file to a stored process from python/php/other backend in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Upload-file-to-a-stored-process-from-python-php-other-backend/m-p/240167#M4789</link>
    <description>&lt;P&gt;Ted,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you sure you're setting the headers properly in your POST request from Python? The Content Type needs to be set to multipart/form-data.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://stackoverflow.com/questions/68477/send-file-using-post-from-a-python-script" target="_blank"&gt;http://stackoverflow.com/questions/68477/send-file-using-post-from-a-python-script&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;has an example where the header is set correctly in Python. It also suggests the 'poster' library which seems pretty nifty, rather than doing it all manually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you submit a form with an upload component from the browser, as per the STP form with input type "file", I think the browser automatically sets the header of Content-type: multipart/form-data. This is how SAS will know that you're uploading a file as part of the post request. Otherwise, the default content type in the header for a POST is set to application/x-www-form-encoded (I think).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As per top answer from &lt;A href="http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean:" target="_blank"&gt;http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean:&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;When you make a POST request, you have to encode the data that forms the body of the request in some way.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;HTML forms provide three methods of encoding.&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT color="#339966"&gt;&lt;CODE&gt;application/x-www-form-urlencoded&lt;/CODE&gt; (the default)&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT color="#339966"&gt;&lt;CODE&gt;multipart/form-data&lt;/CODE&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT color="#339966"&gt;&lt;CODE&gt;text/plain&lt;/CODE&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;Work was being done on adding &lt;A href="http://www.w3.org/TR/html-json-forms/" target="_blank"&gt;&lt;CODE&gt;application/json&lt;/CODE&gt;&lt;/A&gt;, but that has been abandoned.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;The specifics of the formats don't matter to most developers. The important points are:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;When you are writing client-side code, all you need to know is &lt;STRONG&gt;use &lt;CODE&gt;multipart/form-data&lt;/CODE&gt; when your form includes any &lt;CODE&gt;&amp;lt;input type="file"&amp;gt;&lt;/CODE&gt; elements&lt;/STRONG&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;When you are writing server-side code: &lt;STRONG&gt;Use a prewritten form handling library&lt;/STRONG&gt; (e.g. Perl's &lt;CODE&gt;CGI-&amp;gt;param&lt;/CODE&gt; or the one exposed by PHP's &lt;CODE&gt;$_POST&lt;/CODE&gt; superglobal) and it will take care of the differences for you. Don't bother trying to parse the raw input received by the server.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;Never use &lt;CODE&gt;text/plain&lt;/CODE&gt;.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nik&lt;/P&gt;</description>
    <pubDate>Sun, 20 Dec 2015 15:42:15 GMT</pubDate>
    <dc:creator>boemskats</dc:creator>
    <dc:date>2015-12-20T15:42:15Z</dc:date>
    <item>
      <title>Upload file to a stored process from python/php/other backend</title>
      <link>https://communities.sas.com/t5/Developers/Upload-file-to-a-stored-process-from-python-php-other-backend/m-p/238162#M4788</link>
      <description>&lt;P&gt;I've seemingly read through all the threads and documentation with no avail.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to send a csv&amp;nbsp;from one non-SAS&amp;nbsp;server to a SAS server through a stored process which will return some output. This appears to be trivial to do when using non-file parameters, but impossible when using a csv.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance, assuming a python non-SAS backend, using the requests library I logon and then call the testprint_sp stored process.&lt;/P&gt;&lt;PRE&gt;base_url = "https://mybaseurl"
login_url = base_url + "/SASLogon/login"
s = requests.Session()
r = s.get(login_url, verify=False)

data = {"username": "uname", "password":"pwd123"}
r = s.post(login_url, data=data)

execute_url = base_url + "/SASStoredProcess/do?_program=/User Folders/my folder/testprint_sp"
data = {'num':20}
r = s.post(execute_url, data=data)&lt;/PRE&gt;&lt;P&gt;Or after logging on, simply browsing to&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;https://mybaseurl/SASStoredProcess/do?_program=/User Folders/my folder/testprint_sp&amp;amp;num=20&lt;/PRE&gt;&lt;P&gt;This is great but it fails when I try to send a csv.&lt;/P&gt;&lt;PRE&gt;filename = "D:/Documents/testfile.csv"
files = {'file': ('myfile', open(filename, 'rb'))}
r = s.post(execute_url, data=data, files=files)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;All&amp;nbsp;of the _WEBIN_% macro variables are missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, there is a workaround which is to build an html form within a sas stored process.&lt;/P&gt;&lt;PRE&gt;&amp;lt;form action="StoredProcessWebApplicationURL" method="post"
 enctype="multipart/form-data"&amp;gt;
&amp;lt;input type="hidden" name="_program" value="/Path/StoredProcessName"&amp;gt;
&amp;lt;table border="0" cellpadding="5"&amp;gt;
 &amp;lt;tr&amp;gt;
 &amp;lt;th&amp;gt;Choose a file to upload:&amp;lt;/th&amp;gt;
 &amp;lt;td&amp;gt;&amp;lt;input type="file" name="myfile"&amp;gt;&amp;lt;/td&amp;gt;
 &amp;lt;/tr&amp;gt;
 &amp;lt;tr&amp;gt;
 &amp;lt;td colspan="2" align="center"&amp;gt;&amp;lt;input type="submit" value="OK"&amp;gt;&amp;lt;/td&amp;gt;
 &amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;&lt;/PRE&gt;&lt;P&gt;But this adds the extra step of having a client using the form and browsing to a specific file. I'd like the data to be automatically sent to SAS without having an html form built in a SAS procedure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Dec 2015 21:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Upload-file-to-a-stored-process-from-python-php-other-backend/m-p/238162#M4788</guid>
      <dc:creator>TedP</dc:creator>
      <dc:date>2015-12-07T21:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: Upload file to a stored process from python/php/other backend</title>
      <link>https://communities.sas.com/t5/Developers/Upload-file-to-a-stored-process-from-python-php-other-backend/m-p/240167#M4789</link>
      <description>&lt;P&gt;Ted,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you sure you're setting the headers properly in your POST request from Python? The Content Type needs to be set to multipart/form-data.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://stackoverflow.com/questions/68477/send-file-using-post-from-a-python-script" target="_blank"&gt;http://stackoverflow.com/questions/68477/send-file-using-post-from-a-python-script&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;has an example where the header is set correctly in Python. It also suggests the 'poster' library which seems pretty nifty, rather than doing it all manually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you submit a form with an upload component from the browser, as per the STP form with input type "file", I think the browser automatically sets the header of Content-type: multipart/form-data. This is how SAS will know that you're uploading a file as part of the post request. Otherwise, the default content type in the header for a POST is set to application/x-www-form-encoded (I think).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As per top answer from &lt;A href="http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean:" target="_blank"&gt;http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean:&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;When you make a POST request, you have to encode the data that forms the body of the request in some way.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;HTML forms provide three methods of encoding.&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT color="#339966"&gt;&lt;CODE&gt;application/x-www-form-urlencoded&lt;/CODE&gt; (the default)&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT color="#339966"&gt;&lt;CODE&gt;multipart/form-data&lt;/CODE&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT color="#339966"&gt;&lt;CODE&gt;text/plain&lt;/CODE&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;Work was being done on adding &lt;A href="http://www.w3.org/TR/html-json-forms/" target="_blank"&gt;&lt;CODE&gt;application/json&lt;/CODE&gt;&lt;/A&gt;, but that has been abandoned.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;The specifics of the formats don't matter to most developers. The important points are:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;When you are writing client-side code, all you need to know is &lt;STRONG&gt;use &lt;CODE&gt;multipart/form-data&lt;/CODE&gt; when your form includes any &lt;CODE&gt;&amp;lt;input type="file"&amp;gt;&lt;/CODE&gt; elements&lt;/STRONG&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;When you are writing server-side code: &lt;STRONG&gt;Use a prewritten form handling library&lt;/STRONG&gt; (e.g. Perl's &lt;CODE&gt;CGI-&amp;gt;param&lt;/CODE&gt; or the one exposed by PHP's &lt;CODE&gt;$_POST&lt;/CODE&gt; superglobal) and it will take care of the differences for you. Don't bother trying to parse the raw input received by the server.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;Never use &lt;CODE&gt;text/plain&lt;/CODE&gt;.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nik&lt;/P&gt;</description>
      <pubDate>Sun, 20 Dec 2015 15:42:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Upload-file-to-a-stored-process-from-python-php-other-backend/m-p/240167#M4789</guid>
      <dc:creator>boemskats</dc:creator>
      <dc:date>2015-12-20T15:42:15Z</dc:date>
    </item>
  </channel>
</rss>

