Network request fails, but only when exported to APK

How do I…

How do I get a network request to my own server to work, even when exporting to APK?

I have distilled the simplest “GET” request in GDevelop, simplified from this simple example that queries the Star Wars server: Parse json from api - a game example from the GDevelop game making app | GDevelop

Pointed at my server:

Pointed at the Star Wars server:

As you can see, it’s the exact same code. The second two lines are literally the exact same, it’s just for context. The only difference is that the first line of code points to my server instead of the Star Wars server.

What is the expected result

The expected result is that the text object is populated by the JSON contents that has been requested, in this case the variable “id” which has the value “123”. This happens just fine when exported to Web or Linux. The text object shows up the contents of the JSON results “123”. The issue is that, when exported to APK, it does not exhibit the same behavior.

You can see the JSON works fine: https://tilde.camp/chat/secret

What is the actual result

For some reason, when the code is pointed at my server, and it’s exported to APK, then when you try to receive the “123” from the server it just shows up as “0”.

Stranger still, I can’t duplicate the error with the Star Wars server. The exact same line of code pointed at a different server actually works fine, even when exported to an APK.

The most obvious possibility is that something is wrong with my server, but then why would it work when exported to Web or Linux?

Previews and PC builds send requests to web servers directly, as e.g. cURL would. However, Mobile and HTML5 builds leverage the browser’s web requests.

Browsers “ask” other websites via an OPTIONS http request if the current website is allowed to send a request before sending a request from one site to another. That is to prevent a website from e.g. sending a request from your IP address and with your logged-in browser to your bank’s API to transfer themselves money. This is called CORS.

You would not have noticed CORS issues since they are ignored by previews and PC builds, but when going to Mobile/HTML5 if your web server does not allow explicitly requests to be made via the corrrect CORS headers, the request will be blocked.

Thanks so much for pointing me in the right direction! You’re right, the HTML5 export also has the same trouble. It displays “0” instead of “123” from my server, but displays from the Star Wars server just fine. But I’ve added “Access-Control-Allow-Origin: *;” to my nginx server configuration, restarted the server, and you can see that the “Access-Control-Allow-Origin: *;” actually does show up when you use firefox and go to https://tilde.camp/chat/secret it’s displayed in the “Headers” tab. So why would it still not work? Any ideas? :thinking: I followed this How to Enable CORS in NGINX - Ubiq BI for my nginx server, and it seems pretty simple and straight-forward so I’m not sure what I could be missing.