HTTP POST请求/响应

时间:2020-07-28 06:02:38

标签: python json api post response

当我发出POST请求时,如何访问键的值(成功) 我尝试了以下

    @app.route('/register', methods=['GET','POST'])
 def vid():
 if request.method == "POST":
  firstname=request.form['firstname']
  lastname=request.form['lastname']
  email=request.form['email']
  gender=request.form['gender']
  phone=request.form['mobileno']
  city=request.form['city']
  dob=request.form['dob']
  qualification=request.form['qualification']
  marital=request.form['marital']
  occupation=request.form['occupation']
  logintype=request.form['logintype']
  password=request.form['password']
  redemptionmode=request.form['redemptionmode']
  headers={'X-AUTH': 'GJTREWFRBN8675'}
  inputs={'firstname': firstname , 'lastname': lastname,'email': email,'gender': 
   gender,'mobile':phone, 'city': city,
  'dob': dob,'education': 
  occupation,'maritalstatus':marital,'occupation':occupation,'logintype':'local','password':password,
  'redemptionmode': redemptionmode}
  r = requests.post('https://api.imaginedreality.in/api/signup',headers=headers,data=inputs)
  
  if r.success == "true":
     #some operation#
  else 
     #some operations#

''' 出现错误“对象响应没有属性成功”,但是有一个名为成功的密钥可以访问其值吗?

这是理想的答案

{"success": "true","data": { },"message": "Signup Success"}

1 个答案:

答案 0 :(得分:0)

您必须使用将其转换为json来获取数据 删除if-else,您可以编写如下内容。

r = r.json()

if(r['success'] == 'true'):
    # Some operation
else:
    # Some operation

谢谢