# Send Audit Log via CURL in background

**URL:** <https://the.fmsoup.org/t/send-audit-log-via-curl-in-background/3826>\
**Category:** MBS Plugins\
**Tags:** integration, plugin, mbs\
**Created:** [December 1, 2023, 6:46am UTC](https://the.fmsoup.org/t/send-audit-log-via-curl-in-background/3826 "2023-12-01T06:46:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/monkeybreadsoftware/32/361_2.png) [@MonkeybreadSoftware](https://the.fmsoup.org/u/MonkeybreadSoftware)\
**Post date:** [December 1, 2023, 6:46am UTC](https://the.fmsoup.org/t/send-audit-log-via-curl-in-background/3826/1 "2023-12-01T06:46:12Z")

</div>

When we saw the new OnWindowTransaction, we got the idea to pass the JSON we receive to a web server to log the changes outside the FileMaker database. We had a similar thing with using [MongoDB](https://www.mbsplugins.eu/component_MongoDB.shtml) functions in a recent blog post: **OnWindowTransaction and MongoDB**. By storing the JSON outside, we can do the audit log on a different server. Whether you pass the JSON to another FileMaker Pro via our [WebHook](https://www.mbsplugins.eu/component_WebHook.shtml) functions, a PHP script on your web server or directly send it to AWS to put in a database.

From the point of view of [MBS FileMaker Plugin](https://www.monkeybreadsoftware.com/filemaker/) we just use the [CURL](https://www.mbsplugins.eu/component_CURL.shtml) functions. And there we have the [CURL.PerformInBackground](https://www.mbsplugins.eu/CURLPerformInBackground.shtml) function to perform a HTTP Post in the background without slowing down the GUI. When the request is sent, we get either an expression evaluated or a script triggered. There we may check if the transfer worked, maybe do something special if it failed. But on the end we always free the curl object.

Take a look on the script:

```auto
Set Variable [$json ; Value: Get(ScriptParameter)] 
Set Variable [$URL ; Value: "https://yourdomain.com/yourscript.php"] 
# 
Set Variable [$curl ; Value: MBS("CURL.New")] 
# configure transfer
Set Variable [$r ; Value: MBS("CURL.SetOptionURL"; $curl; $URL)] 
Set Variable [$r ; Value: MBS("CURL.SetOptionPostFields"; $curl; $json)] 
Set Variable [$r ; Value: MBS("CURL.SetOptionHTTPHeader"; $curl; "Content-Type: application/json")] 
# 
# Let the evaluate then free the transfer
# Set Variable [$r ; Value: MBS("CURL.SetFinishedEvaluate"; $curl; "MBS(\"CURL.Release\"; $$ID$$)" )] 
# or run a script to handle result
Set Variable [$r ; Value: MBS("CURL.SetFinishedScript"; $curl; Get(FileName); "Transfer Finished" )] 
# 
# run in background thread asynchronously
Set Variable [$r ; Value: MBS("CURL.PerformInBackground"; $curl)] 

```

You can have multiple transfers running parallel and no user has to wait for a script to finish, since it ends with the call to [CURL.PerformInBackground](https://www.mbsplugins.eu/CURLPerformInBackground.shtml). With the finish script or expression, we can inspect the result and free the curl session. Since curl has a connection cache, it may reuse the connection automatically. Whether you use the finished script, you get the $curl as parameter. In the evaluate, you use $$ID$$ as placeholder for the plugin to put the $curl value. Of course you can use a Let statement to bundle multiple calls there. In both cases you can check response code, the error code, the effective URL and other values. If you like, you can use [CURL.SetTag](https://www.mbsplugins.eu/CURLSetTag.shtml) in first script and [CURL.GetTag](https://www.mbsplugins.eu/CURLGetTag.shtml) in second script to pass whatever data you need.

Here is the sample Transfer Finished script:

```auto
Set Variable [$curl ; Value: Get(ScriptParameter)] 
# 
# check result?
Set Variable [$Error ; Value: MBS( "CURL.ErrorCode"; $curl )] 
Set Variable [$ResponseCode ; Value: MBS( "CURL.GetResponseCode"; $curl )] 
Set Variable [$EffectiveURL ; Value: MBS( "CURL.GetEffectiveURL"; $curl )] 
# 
Set Variable [$r ; Value: MBS("Curl.Release"; $curl)] 

```

If the script doesn't run, please check your privilege set for the option "Validate cross-file plug-in access (fmplugin)" and make sure it is checked to allow the script trigger.

The example file will be included in the next pre-release of [MBS FileMaker Plugin](https://www.monkeybreadsoftware.com/filemaker/). Please do not hesitate to contact us with your questions.
