La cadena creada por Axios devuelve 403 prohibido ya que la URL se rompe cuando la URL se crea combinando varias cadenas.
Aquí hay un ejemplo: Esto funciona bien:
const inventory = await axios.get("http://steamcommunity.com/inventory/76561198164672016/730/2?l=english&count=5000");Devuelve los resultados correctos.
Pero cuando se usa la misma URL, simplemente combinando diferentes valores, no funciona.
const testId = 76561198164672016; const path = "http://steamcommunity.com/inventory/"+testId.toString()+"/730/2?l=english&count=5000" const inventoryWithPath = await axios.get(path);Esto devuelve un error. No entiendo cómo puede suceder esto cuando las cadenas son las mismas... Aquí está el código completo.
const axios = require('axios').default; const testId = 76561198164672016; async function getInventory(){ try{ //invetory works fine and returns correct values const inventory = await axios.get("http://steamcommunity.com/inventory/76561198164672016/730/2?l=english&count=5000"); //Even though this has the same URL it returns an error. Why and how can I request based on the path? const path = "http://steamcommunity.com/inventory/"+testId.toString()+"/730/2?l=english&count=5000" const inventoryWithPath = await axios.get(path); //console.log(inventoryWithPath) console.log(inventory) return inventory; } catch(err){ console.log(err); console.log("Error happened") } } module.exports={getInventory}Como se sugiere en los comentarios, la razón por la que su código no funciona es porque testId > Number.MAX_SAFE_INTEGER . Lo que puedes hacer para resolver esto es usar BigInt así:
const path = "http://steamcommunity.com/inventory/" + BigInt(testId).toString() + "/730/2?l=english&count=5000"