how to parse key value pair request from url using python requests in flask
<p>I have spent about a week on this issue and although I have made considerable progress I am stuck at a key point.</p>

<p>I am writing a simple client-server program in Python that is supposed to accept key/value pairs from the command line, formulate them into an url, and request the url from the server. The problem appears to be either that the url is not properly formatted or that the server is not parsing it correctly. (That is, the key-value pairs appear to be properly making it from the command line to the function that contains the request.)</p>

<pre><code>import sys
import requests

server_url = "http://0.0.0.0:5006"

def test(payload):

   print('payload in function is ' + payload)
   r = requests.get('http://0.0.0.0:5006/buy', params=payload)
   print(r.url) #debug
   print(r.encoding)
   print(r.text)
   print(r.headers)

if __name__ == '__main__':

    payload = sys.argv[2]
    print('payload from cli is ' + payload)
    test(payload)
</code></pre>

<p>Server:</p>

<pre><code>import subprocess
from flask import Flask
from flask import request
import request

# Configure the app and wallet
app = Flask(__name__)
@app.route('/buy', methods=['GET', 'POST'])

def test(key1, key2):

    key1 = str(request.args.get('key1'))
    key2 = str(request.args.get('key2'))
    print('keys are' + key1 + key2)
    fortune = subprocess.check_output(['echo', 'key1'])
    return fortune

# Initialize and run the server
if __name__ == '__main__':

    app.run(host='0.0.0.0', port=5006)
</code></pre>

<p>Client console output:</p>

<pre><code>payload from cli is {"key1": "foo", "key2": "bar"}
payload in function is {"key1": "foo", "key2": "bar"}
http://0.0.0.0:5006/buy?%7B%22key1%22:%20%22foo%22,%20%22key2%22:%20%22bar%22%7D
ISO-8859-1
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"&gt;
&lt;title&gt;500 Internal Server Error&lt;/title&gt;
&lt;h1&gt;Internal Server Error&lt;/h1&gt;
&lt;p&gt;The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.&lt;/p&gt;

{'Content-Type': 'text/html', 'Content-Length': '291', 'Date': 'Fri, 09 Sep 2016 22:16:44 GMT', 'Server': 'Werkzeug/0.11.10 Python/3.5.2'}
</code></pre>

<p>Server console output:</p>

<pre><code>* Running on http://0.0.0.0:5006/ (Press CTRL+C to quit)
[2016-09-09 18:30:33,445] ERROR in app: Exception on /buy [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
TypeError: test() missing 2 required positional arguments: 'key1' and 'key2'
127.0.0.1 - - [09/Sep/2016 18:30:33] "GET /buy?%7B%22key1%22:%20%22foo%22,%20%22key2%22:%20%22bar%22%7D HTTP/1.1" 500 -
</code></pre>

<p>I take this as showing that the subprocess call is for some reason not able to decipher key1 and key2 from the URL so is failing when it runs "fortune None None".</p>

<p><code>test</code> is expecting values for <code>key1</code> and <code>key2</code>. Flask would provide those through your route. </p>

<pre><code>@app.route('/buy/&lt;key1&gt;/&lt;key2&gt;')
def test(key1, key2):
</code></pre>

<p>Visiting <code>/buy/value1/value2</code> would give values to the arguments. You want to pass values through the query string though. </p>

<p>You just need to remove them from the function's signature. </p>

<pre><code>@app.route('/buy', methods=['GET', 'POST'])
def test():
</code></pre>

python flask python-requests