title: Using Python Requests With Siteminder
body: <p>I am having some trouble using Requests to retrieve some data from a webpage. It uses Siteminder and the initial form only has three fields but when I submit it, my password is changed to hex and other fields are added. Can't seem to get it to work at all. I keeps returning an error message.</p>
  
  <p>Any help is appreciated and I apologize for the long post!</p>
  
  <p>Edit: included the two JavaScript functions because they change the data.</p>
  
  <p>Python:</p>
  
  <pre><code>from requests import session
  with session() as s:
  
      headers = {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
      }
      payload = {
          'USER': 'username',
          'PASSWORD': 'pw',
          'TARGET': 'https://www.THISSITE.com/pg'
      }
  
      resp = s.post('https://www.THISSITE.com/THIS/fcc/THISSITE.fcc', headers=headers, data=payload )
      html = resp.text
      print(html)
  </code></pre>
  
  <p>Form:</p>
  
  <pre><code>&lt;form
  id="login"
  method="post"
  name="Login"
  action="https://www.THISSITE.com/THIS/fcc/THISSITE.fcc"&gt;
  &lt;input
      type="hidden"
      name="TARGET"
      value="https://www.THISSITE.com/pg"
  &gt;&lt;/input&gt;
  &lt;div class="form-group"&gt;
      &lt;input
          type="text"
          id="USER"
          name="USER"
          value=""
      &gt;&lt;/input&gt;
  &lt;div class="form-group"&gt;
      &lt;input
          type="password"
          id="PASSWORD"
          name="PASSWORD"
          value=""
      &gt;&lt;/input&gt;
  &lt;/div&gt;
  &lt;input
      type="submit"
      name="OK"
      value="Login"
      onclick="submitAuthForm(this.form);"
  &gt;&lt;/input&gt;
  </code></pre>
  
  <p></p>
  
  <p>submitAuthForm(form):</p>
  
  <pre><code>function submitAuthForm(form) {
  
      var strval = form.PASSWORD.value;
  
      if(!isJson(strval)){
          var info = {};
          info["Password"] = hexEncode(strval);
          form.PASSWORD.value = JSON.stringify(info);
      }
  }
  </code></pre>
  
  <p>hexEncode(str):</p>
  
  <pre><code>function hexEncode(s){
  
      var chrsz   = 8;
      var hexcase = 0;
  
  function str2binb (str) {
          var bin = Array();
          var mask = (1 &lt;&lt; chrsz) - 1;
          for(var i = 0; i &lt; str.length * chrsz; i += chrsz) {
              bin[i&gt;&gt;5] |= (str.charCodeAt(i / chrsz) &amp; mask) &lt;&lt; (24 - i%32);
          }
          return bin;
      }
  
      function Utf8Encode(string) {
          string = string.replace(/\r\n/g,"\n");
          var utftext = "";
  
          for (var n = 0; n &lt; string.length; n++) {
  
              var c = string.charCodeAt(n);
  
              if (c &lt; 128) {
                  utftext += String.fromCharCode(c);
              }
              else if((c &gt; 127) &amp;&amp; (c &lt; 2048)) {
                  utftext += String.fromCharCode((c &gt;&gt; 6) | 192);
                  utftext += String.fromCharCode((c &amp; 63) | 128);
              }
              else {
                  utftext += String.fromCharCode((c &gt;&gt; 12) | 224);
                  utftext += String.fromCharCode(((c &gt;&gt; 6) &amp; 63) | 128);
                  utftext += String.fromCharCode((c &amp; 63) | 128);
              }
  
          }
  
          return utftext;
      }
  
      function binb2hex (binarray) {
          var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
          var str = "";
          for(var i = 0; i &lt; binarray.length * 4; i++) {
              str += hex_tab.charAt((binarray[i&gt;&gt;2] &gt;&gt; ((3 - i%4)*8+4)) &amp; 0xF) +
              hex_tab.charAt((binarray[i&gt;&gt;2] &gt;&gt; ((3 - i%4)*8  )) &amp; 0xF);
          }
          return str;
      }
  
      s = Utf8Encode(s);
      return binb2hex(str2binb(s));
  }
  </code></pre>
  
  <p>Parameters when I submit via webpage:</p>
  
  <pre><code>SMENC: UTF-8
  SMLOCALE: US-EN
  target: https://www.THISSITE.com/pg
  smauthreason: 27
  smagentname: mR2h1e4BPUPZ5eTpyZckvJXpXO1mE5RpNTYtnh9C8sMfqiHlbrnBjW2SNjbwIRz+
  type: 
  realmoid: 
  smusermsg: 
  USER: username
  PASSWORD: {"TokenId":"longstringoflettersandnumbersHEX???","Password":""}
  </code></pre>