Sending HTTP POST request to a server

Hi!

I’d like to send POST and GET requests to an external server in my PlayCanvas script, to load some additional data at runtime. Unfortunately, I cannot figure out how.

I came across this class in the API reference:
https://developer.playcanvas.com/en/api/pc.Http.html

But somehow I can’t get that to work. Could someone please make a simple example on how to use those functions? Much appreciated.

Do you have a sample project that people can look at and what errors/issues are you getting?

Hi yaustar,

Unfortunately, the project I am working on is private. I can set up an empty project to illustrate what I’m doing if that helps. In the meantime, this is the code I am trying to run:

pc.http.post(url, {email: '...', password: '...'}, function (err, data) {
        console.log(data);
    });

I just noticed right now that I’m getting a CORS error in the console that I missed before. Here’s the error message:
“Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at (url). (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).”

Do you know how I can prevent this from happening? I assume I need to do something on the PlayCanvas side, since I do not have control over the server I’m attempting to connect to.

If it’s a public API, it should be fine. A CORs error suggests that it isn’t public or that the API is expected to be ran from a server and not on the client.

What API/server are you trying to access?

We’re using Directus to store our data. We’re letting them host it (not the open source version), so I don’t think I have access to any of the php configuration. Is this something that has to be done serverside, then?

Edit: the actual URL I’m calling is “https://catatonic–eyesync.directus.io/api/1.1/auth/request-token” (catatonic–eyesync is our project name)

This is something you would have to check with them unfortunately.

Yeah sorry, I figured you wouldn’t be able to help if it really is serverside. Thanks for trying. :slight_smile: