Python: Issue with POST data in Requests
<p>I'm trying to use the <strong>Requests 2.9.1</strong> in <strong>Python 2.7</strong> to learn how to send <strong>POST</strong> data.  However I feel that I'm missing something in the data I'm providing to <strong>requests</strong>, or somehow misformating something and have been banging my head at it all night.</p>

<p>The site I'm basing the form data off of is : <a href="http://qpublic9.qpublic.net/la_orleans_address.php" rel="nofollow">http://qpublic9.qpublic.net/la_orleans_address.php</a></p>

<p>I used Charles (<a href="https://www.charlesproxy.com" rel="nofollow">https://www.charlesproxy.com</a>) to see the actual content of the POST data, and here is what I saw.  The data I entered was <strong>1013</strong> for the <strong>Street Number</strong> (field is <strong>streetNumber</strong>) and ST ANN (relying on the autocomplete) for the *Street Name** (<strong>streetName</strong> is the field here).  This is all I needed to enter on the website manually, and I received this POST data from Charles:</p>

<pre><code>BEGIN=0&amp;searchType=address_search&amp;streetNumber=1013&amp;streetName=ST+ANN&amp;streetType=&amp;streetDirection=&amp;streetUnit=&amp;Address+Search=Address+Search
</code></pre>

<p>Originally I was trying with just the <strong>streetNumber</strong> and <strong>streetName</strong>, since that was the only data I input when testing from the website, but this produced an error.  Now I'm entering null fields for the data that has nothing entered, I'm guessing this might be causing the problem but I'm not sure.  Below is what I'm using for code, followed by the error message I'm receiving (which is an SQL error message, making me think there's something wrong with the way I'm formatting the data payload perhaps).</p>

<pre><code>import requests

payload = {'searchType': 'address_search', 'streetNumber': '1013', \
'streetName': 'ST+ANN', 'streetType': '', 'streetDirection': '', \
'streetUnit': '', 'Address+Search': 'Address+Search' }

response = requests.post("http://qpublic9.qpublic.net/la_orleans_alsearch.php", \
data=payload)

print response.text
</code></pre>

<p>The output and error:</p>

<pre><code>&lt;LINK REL=stylesheet HREF="http://www.qpublic.net/la/neworleans/sytle.css" 
TYPE="text/css"&gt;
Could Not Read Data:You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the
right syntax to use near ' 100' at line 1
</code></pre>

<p>Since I'm just a user, and not a developer for the site, I don't have access to see what their backend code looks like, but since it works from the site in my browser I'm guessing it's something to do with my <strong>payload</strong> formatting rather than their code.</p>

<p>Any suggestions on what I might be missing?  Or any experience with similar problems?  No matter how many empty fields I remove I end up with that same error.</p>

<p>Thanks!</p>

<p><strong>EDIT:</strong> In my code I'm not using the backslashes for the python code, I'm using that here for easier reading.  Also the error message doesn't have line breaks in it, again just for readability.  Since I'm not familiar with the structure of POST data I'm leaving that as is from Charles in case changing something might cause a miscommunication.</p>

<p><strong>EDIT 2:</strong> Also I'm receiving a response code of 200 (<strong></strong>) when I test for that, so it doesn't appear to be an error with the connection over HTTP, so again that makes me think I'm missing something with the POST data.</p>

<p>Take out all the <code>+</code> characters that you put between words. That's the URL-encoded representation of space, but the <code>requests</code> module does that for you. If you use it explicitly, it will re-encode the <code>+</code>, so the server script will see it as a literal <code>+</code>, not the space that it should be decoded to.</p>

<p>You're also missing the <code>BEGIN=0</code> parameter in <code>payload</code>. In my tests, that seems to be the cause of the error you're getting.</p>

<pre><code>payload = {'BEGIN': '0', 'searchType': 'address_search', 'streetNumber': '1013', \
            'streetName': 'ST ANN', 'streetType': '', 'streetDirection': '', 
            'streetUnit': '', 'Address Search': 'Address Search' }
</code></pre>

<p>If you look in the web page source, that's one of the hidden inputs:</p>

<pre><code>&lt;INPUT TYPE=HIDDEN NAME=BEGIN VALUE="0"&gt;
&lt;INPUT TYPE=HIDDEN NAME="searchType" VALUE="address_search"&gt;
</code></pre>

python python-2.7 http-post python-requests