代码之家  ›  专栏  ›  技术社区  ›  Pop Bob

Api返回UnsupportedMediaType

  •  0
  • Pop Bob  · 技术社区  · 11 月前

    我无法使用json将数据插入到api调用中。

    我尝试了一些简单的代码,但它不起作用

    import json
    import requests
    
    url = "https://presence.roblox.com/v1/presence/users"
    body = json.dumps({"userIds": [1]})
    
    response = requests.post(url, data=body)
    presence = response.json()
    
    print(presence)
    
    
    

    我试图设置一个参数“userIds”,这样我就可以列出我想用API检查的id,但我只是感到困惑。

    错误消息:{'错误':[{'代码':0,'消息':'不支持的媒体类型'}]}

    1 回复  |  直到 11 月前
        1
  •  0
  •   Vinod Baste    11 月前

    服务器要求请求正文采用JSON格式。您必须指定内容类型。

    import json
    import requests
    
    url = "https://presence.roblox.com/v1/presence/users"
    
    # Specify JSON content type
    headers = {
        'Content-Type': 'application/json'
    }
    
    body = json.dumps({"userIds": [1]})
    
    response = requests.post(url, headers=headers, data=body)
    presence = response.json()
    
    print(presence)