weixin_33717298 2019-01-08 15:38 采纳率: 0%
浏览 32

IGDB V3请求

Does anyone know how to properly search for games using the Internet Game Database API as of version 3? I'm trying to use IGDB to do a simple game search. For each game that matches the search terms, I'd like to retrieve the game's name, genres, developers and publishers, it's initial release date, and the URL of the cover. Through some Googling I'd gotten it working through the old URL parameters, but something seems to have changed on their end, and those no longer work. The version 3 documentation says to use Apicalypse to send the fields you want back in the body of the web request, but I can't for the life of me figure out how to do that. I'm using AJAX to send the search terms to the controller, and sending the results back via a JSON object. I'm getting a 400 Bad Request error every time, no matter the syntax I use for the fields. The documentation says that using URL parameters should still work, but they do not. Here's my controller code.

[HttpPost]
    [WebMethod]
    public JsonResult LookUpGames(string search)
    {
        string url = "https://api-v3.igdb.com/games/?search=" + search
            + "&fields=name,genres,involved_companies,first_release_date,cover";
        HttpWebRequest gameRequest = (HttpWebRequest)WebRequest.Create(url);
        gameRequest.Accept = "application/json";
        gameRequest.Headers.Add("user-key", "[MYUSERKEY]");
        WebResponse gameResponse = (HttpWebResponse)gameRequest.GetResponse();
        string responseString = new StreamReader(gameResponse.GetResponseStream()).ReadToEnd();
        return Json(new { result = responseString });
    }

UPDATE: Thanks for the pointer, Jake. I'm now hitting the servers with the following code.

HttpResponse<JsonResult> jsonResponse = Unirest.post("https://api-v3.igdb.com/games")
                .header("user-key", "[MYUSERKEY]")
                .header("Accept", "application/json")
                .body("fields name,genres,platforms,involved_companies,cover").asJson<JsonResult>();
JsonResult jsonResult = Json(new { result = jsonResponse });
            return jsonResult;

There is no JsonNode in C# apparently, so I tried JsonResult, and the .asJson() seems to be .asJson(). I just fiddled with it until it worked. But I'm still not getting back a list. I'm getting a 400 Bad Request error. So even in this new format, it's still not liking the fields I'm giving it. According to the structure in the documentation, the fields I'm giving it are in fact there in the Game endpoint. So I don't know what could be wrong. Any other ideas anyone?

  • 写回答

1条回答 默认 最新

  • weixin_33694620 2019-01-14 21:42
    关注

    I decided to try the query approach again, and somehow it now works. Here's my controller method that works. Not sure which tweaks made it work again, but it does.

    [HttpPost]
    [WebMethod]
    public JsonResult LookUpGames(string search)
    {
        string url = "https://api-v3.igdb.com/games?search=" + search + 
            "&fields=name,genres.name,platforms.name,involved_companies.*, involved_companies.company.*,first_release_date,cover.url";
        HttpWebRequest gameRequest = (HttpWebRequest)WebRequest.Create(url);
        gameRequest.Accept = "application/json";
        gameRequest.Headers.Add("user-key", "[MYUSERKEY]");
        WebResponse gameResponse = gameRequest.GetResponse();
        StreamReader sr = new StreamReader(gameResponse.GetResponseStream());
        string responseString = sr.ReadToEnd();
        sr.Close();
        JsonResult jsonResult = Json(new { result = responseString });
        return jsonResult;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿