0

Here's my code:

#http://silvo/ESPresso.dll/UpdateDevice?DeviceName=BlueForceTag&DeviceMac=&DeviceSerial=ABBF0002&TxCount=4&Lat=-33.751320&Long=18.533993&BattLevel=4.05&RSSI=-94&hdop=1.20&sats=9&interval=600000

from flask import Flask, request

app = Flask(__name__)

@app.route('/ESPresso.dll/UpdateDevice', methods=['GET'])

def index():
    page_response=""
    for arg in request.args:
        page_response+=str(arg)
    return page_response

app.run()

I would like to replace the code in index() to return a text list of all arguments passed in the path with name = value format. e.g. from the example URL at the top:

DeviceName=BlueForceTag
DeviceMac=
DeviceSerial=ABBF0002

etc.

Currently my code returns

DeviceNameDeviceMacDeviceSerialTxCountLatLongBattLevelRSSIhdopsatsinterval

and I can't get hold of the individual "name" / "value" properties of the arg object.

I'm obviously missing something fundamental (probably Python related). Any help appreciated.

Hein du Plessis
  • 3,305
  • 6
  • 34
  • 51

1 Answers1

3

The flask.request.args() method returns a dictionary like object. Please refer to the docs

In keeping with this information, your for loop needs to be modified as follows:

page_response = ''
for key, value in request.args.items():
    page_response += '{0}={1}<br/>'.format(key, value)
return page_response

Or simply:

return "<br/>".join(["{}={}".format(*a) for a in request.args.items()])
Nawaz
  • 353,942
  • 115
  • 666
  • 851
Nitul
  • 369
  • 3
  • 7
  • Thank you, I understood it was something called a MultiDict which I got very little information from. I just changed your result line to page_response += '
    {0}={1}'.format(key, value)
    – Hein du Plessis Sep 21 '20 at 19:20