# How to do the POST API call from babylon?

**URL:** <https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782>\
**Category:** Questions\
**Tags:** documentation, tutorial\
**Created:** [September 13, 2021, 5:36am UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782 "2021-09-13T05:36:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 13, 2021, 5:36am UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/1 "2021-09-13T05:36:00Z")

</div>

Hi,  
I want to do the post api call from babylon, before that i want to save my details as a json format in my babylon script using array or list. Depends on the input i will add or delete those data.  
Then the final data will be go from the babylon POST api.

Can you please tell me how to do this in babylon?

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [September 13, 2021, 8:55am UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/2 "2021-09-13T08:55:22Z")

</div>

Hello !

Babylon is just Javascript. So you should be able to send Http requests in Babylon just the way you would in any plain JS script :

- Gather data
- Listen to an event
- Send a request with xhttp with the stringified data

It could look something like this :

```auto
const myData = {
    name: "example",
    content: "this data has been set somehow in the Babylon app",
    target: undefined, // will be set later
}

// Here you fire the request, based on any Babylon event, for example a pointerDown (mySphere has been declared earlier in the app
mySphere.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickUpTrigger, function () {
     myData.target = mySphere
     xhttp.open("POST", "https://my.endpoint/custom", true);
     xhttp.setRequestHeader("Content-type", "application/json");
     xhttp.send(json.stringify(myData);
}));

```

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 13, 2021, 4:24pm UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/3 "2021-09-13T16:24:38Z")

</div>

Thanks for your reply.  
i tried this part it works good, but unfortunately when i do the post request to my azure i am getting the CORS error.

```auto
myData = {

        transform: "sdas",
		scale:"dasdasd",
		rotation:"fsdvxcv",
		hotspotText:"asdawdasdasdasdasdasd",
		hotspotimage:"",
		hotspotMedia:"",
		createdBy:"Local"
    }
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "my-azure-api-lin", true);
     xhttp.setRequestHeader("Content-type", "application/json");
     xhttp.setRequestHeader("Access-Control-Allow-Origin","http://127.0.0.1:5500/");
     xhttp.setRequestHeader("Access-Control-Allow-Methods","GET, POST");
     xhttp.setRequestHeader("Access-Control-Allow-Headers","*");
     xhttp.setRequestHeader("Access-Control-Allow-Credentials","true");

     xhttp.send(JSON.stringify(myData));

```

When i use this function i am getting below error:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/f/4f4f0edf4db4cce1b30c26016e62fd378a60e9a6.png)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 13, 2021, 4:50pm UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/4 "2021-09-13T16:50:32Z")

</div>

it seems you would need to allow CORS on your azure service 🙂

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [September 13, 2021, 5:01pm UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/5 "2021-09-13T17:01:03Z")

</div>

So, CORS error is a notorious pain in the butt, when trying to developing http requests, although it has nothing to do with Babylon.js.

The error is raised because the endpoint you are targeting doesn’t know where the request is coming from and wether the source can be trusted. **You have to set your trusted domains in your azure server.**  
I don’t know azure at all, but you should look into that. I found [something here](https://stackoverflow.com/questions/43767255/cors-with-azure-function-from-localhost-not-cli), for instance.

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 13, 2021, 5:40pm UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/6 "2021-09-13T17:40:26Z")

</div>

CORS setup is done in azure, when i do the post call from postman it works fine, but when i do from babylon its throwing an error.  
Please correct me if i am wrong.

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 13, 2021, 5:41pm UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/7 "2021-09-13T17:41:21Z")

</div>

SO you are saying i need to add these cors setup in my azure api management where i configured the API domains for the request?

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [September 13, 2021, 6:15pm UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/8 "2021-09-13T18:15:28Z")

</div>

> [@Siva\_s](#):
>
> when i do the post call from postman it works fine, but when i do from babylon its throwing an error.

It’s possible that Postman is a trusted domain by default for Azure, that may explain it. But localhost definitely needs a CORS settings on the server.

[Here you have a video explaning how to set CORS in your Azure app](https://azure.microsoft.com/fr-fr/resources/videos/cors-and-api-management/).

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/a/4/a4c26440d4af689e9bbfc28f84033ba34b011b7a.jpeg)

Try this out and tell me if it works. 🙂

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 14, 2021, 3:59am UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/9 "2021-09-14T03:59:25Z")

</div>

Sure let me try this 🙂

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 14, 2021, 4:39am UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/10 "2021-09-14T04:39:03Z")

</div>

Just tried it worked.  
Not getting the CORS error now thank you 🙂

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [September 14, 2021, 11:46am UTC](https://forum.babylonjs.com/t/how-to-do-the-post-api-call-from-babylon/23782/11 "2021-09-14T11:46:48Z")

</div>

Glad to hear it fixed your problem ! Don’t forget to tag the question as solved for archiving. 😉
