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个回答


我决定再次尝试查询方法,现在它以某种方式起作用。 这是我的控制器方法。 不知道哪些调整使它再次起作用,但是确实可以。</ p>

  [HttpPost]
[WebMethod]
公共JsonResult LookUpGames(字符串搜索)
{
     字符串url =“ https://api-v3.igdb.com/games?search=” +搜索+
         “&fields =名称,流派名称,平台名称,涉及的公司。*,涉及的公司。*,first_release_date,cover.url”;
     HttpWebRequest gameRequest =(HttpWebRequest)WebRequest.Create(url);
     gameRequest.Accept =“ application / json”;
     gameRequest.Headers.Add(“ user-key”,“ [MYUSERKEY]”);
     Web响应游戏:Response = gameRequest.GetResponse();
     StreamReader sr =新的StreamReader(gameResponse.GetResponseStream());
     字符串responseString = sr.ReadToEnd();
     sr.Close();
     JsonResult jsonResult = Json(new {result = responseString});
     返回jsonResult;
}
</ code> </ pre>
     </ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐