1

I am using an HTML form to take in parameters to use for Azure REST API calls.

My Code

@app.route('/storageaccountcreate', methods = ['POST', 'PUT'])
def storageaccountcreate():
    name = request.form['storageaccountname']
    resourcegroup = request.form['resourcegroup']
    subscriptionId = request.form['subscriptionId']
    location = request.form['location']
    sku = request.form['sku']
    keys = [name, resourcegroup, subscriptionId, location, sku]
    api_json = {keys: request.form[key] for key in keys}
    url = 'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2019-06-01'
    r = requests.put((url),data=(api_json))
    print(r.text)
    return r.text

I am getting the following error when trying to use this code

Bad Request
The browser (or proxy) sent a request that this server could not understand.

Also the debugging in VSC is showing the following

Photo of error

If you have any info that would help I would greatly appreciate it!

9
  • Should that url be an f-string? Commented Apr 7, 2020 at 21:57
  • I'm not sure lol that is why I'm asking. I didn't think it should be an f-string as i've not seen any examples with it as an f-string but I'm not sure. If it was a string then I would be curious how it would know where it input the variables into the url. Commented Apr 7, 2020 at 21:59
  • Well what do you expect {subscriptionId} to do in that string? Commented Apr 7, 2020 at 22:00
  • The issue that it is showing in debugging is something with this part api_json = {keys: request.form[key] for key in keys} Commented Apr 7, 2020 at 22:00
  • I would think that subscriptionId in that string would be sticking the subscriptionId variable there. or the key=value Commented Apr 7, 2020 at 22:01

1 Answer 1

2

Python doesn't interpolate values in regular strings:

url = 'https://management.azure.com/subscriptions/{subscriptionId}/...'
#                                                 ^^^^^^^^^^^^^^^^

Assuming you're using Python 3.6 or later, use an f-string to enable interpolation:

url = f'https://management.azure.com/subscriptions/{subscriptionId}/...'
#     ^ here
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.