I was messing around with the network tab in my browser on the multiplayer game room page, trying to make a working request. My goal was to take a game room code (eg. 535FE6
) and return its status (200 if the room exists, or 404 if it doesn’t).
After some fiddling around, I found out that MakeCode sends a request to this URL
mp.makecode.com/api/game/join/{room code}
with some headers to check if the room exists. If the room doesn’t exist, it returns an empty response with a statuscode of 404, and if it exists it returns a join key with a code of 200. I thought I would be able to easily use this for my needs, and I was partially right.
When I copied the request as a fetch method and pasted it into the chrome console, it worked flawlessly. Same thing when I attempted to access it externally. I copied all the headers into Insomnia and sent a GET request which returned one of the expected responses.
However, after attempting to use the fetch method in a JS script for a website locally, it kept returning the same ‘405 Method not allowed’ status code, along with this chrome console error
Access to fetch at ‘https://mp.makecode.com/api/game/join/535FE6’ from origin ‘http://127.0.0.1:5500’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
This is the code I’m using
async function checkCodeValidity(code) {
let response = await fetch(`https://mp.makecode.com/api/game/join/${code}`, {
"headers": {
"accept": "*/*",
"accept-language": "en,hr;q=0.9,bs;q=0.8",
"authorization": "mkcd {Joey just editted this to cut this out because he hates seeing tokens, should be fine with session cookie but never paste anything like this openly pls}",
"sec-ch-ua": "\"Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"115\", \"Chromium\";v=\"115\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
},
"referrer": "https://arcade.makecode.com/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include"
})
.then((response) => alert(response.status));
}
I tried changing the headers, adding new ones, changing the method to no-cors
and it made no difference whatsoever.
Did I miss something in my code or request? If so, could someone help me out with this, and if that’s not the case, maybe I’m approaching this all wrong. Is there a better way to achieve what I’m trying to do @jwunderl? Thanks in advance