代码之家  ›  专栏  ›  技术社区  ›  Luis Ramon Ramirez Rodriguez

烧瓶上的POST请求失败

  •  0
  • Luis Ramon Ramirez Rodriguez  · 技术社区  · 6 年前

    这个问题已经有了答案:

    我正试着从这里复制代码 page ,完整的github代码是 here 以下内容:

    这个应用程序在浏览器上运行得很好,但是我不能从python中复制POST请求。

    当我使用浏览器时,我已经尝试使用与负载上显示的相同数据

    enter image description here

    PEOPLE =  {"fname": "DDoug",
            "lname": "FarDrell"}
    
    url = "http://localhost:5000/api/people"
    data = requests.post(url,data=json.dumps(PEOPLE) )
    

    但我有以下错误:

    data.text
    
    '{\n  "detail": "Invalid Content-type (), expected JSON data",\n  "status": 415,\n  "title": "Unsupported Media Type",\n  "type": "about:blank"\n}\n'
    

    我也这样试过:

    url = "http://localhost:5000/api/people"
    data = requests.post(url,data=json.dumps(PEOPLE) )
    

    但有个错误:

    '{\n  "detail": "Invalid Content-type (application/x-www-form-urlencoded), expected JSON data",\n  "status": 415,\n  "title": "Unsupported Media Type",\n  "type": "about:blank"\n}\n'
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   djnz    6 年前

    添加 Content-Type 在post头中指定要发送json数据:

    requests.post(url,data=json.dumps(PEOPLE), headers={'Content-Type': 'application/json'})
    

    您也可以使用 json 实现相同结果的参数:

    requests.post(url, json=json.dumps(PEOPLE))
    
    推荐文章