0

Because I can't be at work today (post-appendectomy) but still want to work on my PowerShell skills, I decided I would try to access the World of Warcraft API and pull down auction house data for fun.

$apiKey = "myapikey"
$serverName = "MyServer"
$webAddress = "https://us.api.battle.net/wow/auction/data/"

$url = $webAddress + $serverName + "?apikey=" + $apiKey

$response = Invoke-RestMethod -Uri $url -ContentType "application/json"

$aucURL = $response.files.url
$aucTime = $response.files.lastModified

$response = Invoke-RestMethod -Uri $aucURL

$aucData = $response.auctions #Always returns blank

I get to this point and get a huge list of data that looks like below and for the life of me I can't seem to access the auctions data.

{
"realms": [
    {"name":"MyServer","slug":"MyServer"}],
"auctions": [
    {"auc":723774847,"item":109167,"owner":"Laddypally","bid":369550,"buyout":389000,"quantity":1,"timeLeft":"VERY_LONG","rand":0,"seed":0,"context":1},
    {"auc":724234542,"item":2996,"owner":"Mazramtaim","bid":760000,"buyout":800000,"quantity":20,"timeLeft":"VERY_LONG","rand":0,"seed":0,"context":0},
    {"auc":723207271,"item":133563,"owner":"Alsalak","bid":1187500,"buyout":1250000,"quantity":5,"timeLeft":"LONG","rand":0,"seed":0,"context":0}]}

My method of taking $responses.whatever has worked in the past with my work related PS stuff but not here. Any reason why?

Tchotchke
  • 399
  • 1
  • 2
  • 18
  • 2
    [`Invoke-RestMethod`](https://technet.microsoft.com/en-us/library/hh849971.aspx) is supposed to convert JSON responses to objects, and if I do that on your example content then `$x.auctions` works fine. So I guess the server is not sending back anything (e.g. a content-type header) to tell PS to convert it from JSON, and it's considering it as a plain text response. Try converting it yourself with `$response = $response | ConvertFrom-JSON` then `$response.auctions` – TessellatingHeckler Sep 02 '16 at 19:36
  • I tried converting from JSON but because the file being pulled is ~10 Mb it's choking and giving me an error about max file size. I'm looking into this error separately but the answers I've found so far appear messy. I think you're onto something though. The first $response comes back as a PSCustomObject, the second as a String. The file itself is just named "http://auction-api-us.worldofwarcraft.com/auction-data/5bc6c35600c6bc1c75581be4a68b82a4/auctions.json" – Tchotchke Sep 02 '16 at 20:15
  • I would think one of the methods described here would solve the size issue: http://stackoverflow.com/questions/16854057/convertfrom-json-max-length. – Kory Gill Sep 02 '16 at 21:17
  • 1
    I am unable to replicate this issue in PowerShell v5. Maybe you should update your PowerShell version? I just pulled the auction data for Sargeras (19.68MB), and was able to access it with `$AucData`, all 112606 entries. – TheMadTechnician Sep 02 '16 at 21:45
  • @TheMadTechnician, I'll give PS 5 a shot and let you know – Tchotchke Sep 02 '16 at 22:58
  • @TheMadTechnician, updating to v5 fixed the issue. I take it then it wasn't an issue with my code but a limitation with PS 4? – Tchotchke Sep 03 '16 at 03:11

1 Answers1

0

Upgrading to PowerShell 5 fixed the issue. I didn't change any of the code so the issue must've been in how PS4 deals with large JSON requests.

Tchotchke
  • 399
  • 1
  • 2
  • 18