Error with HTTP Post in python requests library
Question: I am not able to send a http post request via python(Flask).

Here is my python code.

    
    
    import json
    from flask import Flask, render_template, request, jsonify
    import requests
    app = Flask(__name__)
    
    
    @app.route("/",methods=['GET','POST'])
    def home():
        if request.method == 'POST':
            #user inputs
            value1 = request.form.get('first')
            value2 = request.form.get('second')
            value3 = request.form.get('third')
    
    
            #api call 
            url = 'http://myapiurl.com/ws/spm/spm-general'
            payload = {"perfid" : {0}, "section" : {"hostname" : {1}, "iteration" : {2}, "sectionname" : "sysstat_M"}.format(value1,value2,value3)}
    
    
    
            r = requests.post(url, data=json.dumps(payload))
    
            #print(r.status_code, r.headers['content-type'])
            #print(r.text)
    
            returnData["result"] = json.loads(r.text)
            return jsonify(returnData)
    
        return render_template('index.html')
    
    
    
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    

I am using FLASK. The error I am getting is :

    
    
     payload = {"perfid" : {0}, "section" : {"hostname" : {1}, "iteration" : {2},
     "sectionname" : "sysstat_M"}.format(value1,value2,value3)}
    AttributeError: 'dict' object has no attribute 'format'
    

When I try it in simple python(without Flask) it works , but I am not able to
do it with Flask

Here is the code which works:

    
    
    import json
    import requests
    
    
    url = 'http://myapiurl.com/ws/spm/spm-general'
    payload = {"perfid" : 124, "section" : {"hostname" : "10.161.146.94/10.161.146.90", "iteration" : "1", "sectionname" : "sysstat_M"}}
    
    r = requests.post(url, data=json.dumps(payload))
    
    
    print(r.status_code, r.headers['content-type'])
    print(r.text)
    


Answer: It seems like you're confused `dict` with `str` (which has a
[`format`](https://docs.python.org/3/library/stdtypes.html#str.format)
method).

Just put the variables to the dictionary literal:

    
    
    payload = {
        "perfid" : value1,
        "section" : {
            "hostname" : value2,
            "iteration" : value3,
            "sectionname" : "sysstat_M"
        }
    }
    

