【问题标题】:HTTP POST request/responseHTTP POST 请求/响应
【发布时间】:2020-11-17 12:42:53
【问题描述】:

当我发出 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"}

【问题讨论】:

  • r.json()['success'] 顺便说一句,阅读文档可能是个好主意。

标签: python json api post response


【解决方案1】:

您必须使用将其转换为json 才能获取数据 去掉 if-else 就可以这样写了。

r = r.json()

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

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    相关资源
    最近更新 更多