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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题