# system.net.httpClient asynchronous use - callback function with user payload?

**URL:** <https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [December 22, 2020, 1:45pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721 "2020-12-22T13:45:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [December 22, 2020, 1:45pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721/1 "2020-12-22T13:45:54Z")

</div>

@paul-griffith, I really appreciate the “new” **system.net.httpClient**

I use it in the asynchronous flavor.

```auto
promise = httpClient.getAsync("http://127.0.0.1:8081/test")
promise.then(callbackPollData)|

...

def callbackPollData(result, error):

```

I juste have a “feature request” or I’ve missed something…  
I would like to be able to use the same callback function for multiple request.

IMO it would be very usefull if we were able to pass a user dictionnary payload to the callback when we register it.

```auto
myPayload = {...}
promise = httpClient.getAsync("http://127.0.0.1:8081/test")
promise.then(callbackPollData,myPayload)

...

def callbackPollData(result, error, payload):

```

callback function is a callable PyObject ? perhaps user payload can be stored in the PyObject, I will try…

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 22, 2020, 1:55pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721/2 "2020-12-22T13:55:11Z")

</div>

Your callback will have access to `myPayload` due to its presence in the local variable namespace when the `def` executes. This is called _ **closure** _. Worth learning to use. Another option is applying python’s `partial` function to yield a function that carries your payload.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [December 22, 2020, 2:26pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721/3 "2020-12-22T14:26:06Z")

</div>

partial function doesn’t work for me as args for promise.then(partial(…))

I don’t want to use closure because it need to nest functions

I finally end with a callback like:

```auto
promise.then(CallBackWithPayload({"data":123}))

...

class CallBackWithPayload():
	def __init__ (self, payload):
		self.payload = payload
	
	def __call__ (self,result,error):
		strMsg = "payload=%s - result=%s - error=%s" % (self.payload,result,error)
...

```

but is there any risk of memory leak with this class if the function **promise.then(…)** is called every x seconds ? I don’t know when CallBackWithPayload instances will be garbage collected…?

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [December 22, 2020, 2:49pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721/4 "2020-12-22T14:49:07Z")

</div>

With adding:

```auto
class CallBackWithPayload():

  def __del__ (self):
    log...

```

it seems to confirm CallBackWithPayload are destroyed after waiting less than 2 minutes.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 22, 2020, 3:16pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721/5 "2020-12-22T15:16:49Z")

</div>

I hope you’re putting the `CallBackWithPayload` class in its own project script…

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [December 22, 2020, 3:46pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721/6 "2020-12-22T15:46:54Z")

</div>

Yes sure !

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 4, 2021, 5:55pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721/7 "2021-01-04T17:55:05Z")

</div>

> [@mazeyrat](#):
>
> partial function doesn’t work for me as args for promise.then(partial(…))

Is it throwing an error? In a few tag related scripting functions we're doing too strict of a check for callable functions, so httpClient might be doing something similar. `partial` would be the recommended way forward...

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [January 4, 2021, 8:55pm UTC](https://forum.inductiveautomation.com/t/system-net-httpclient-asynchronous-use-callback-function-with-user-payload/41721/8 "2021-01-04T20:55:32Z")

</div>

No, there is no error when I call promise.then(partial(myFunction,payload)) but myFunction seem to be never called
