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