# URLConnection used for REST-API

**URL:** <https://forum.xojo.com/t/urlconnection-used-for-rest-api/73123>\
**Category:** General\
**Created:** [November 10, 2022, 1:56pm UTC](https://forum.xojo.com/t/urlconnection-used-for-rest-api/73123 "2022-11-10T13:56:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clemens\_Dähne](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@Clemens\_Dähne](https://forum.xojo.com/u/Clemens_D%C3%A4hne)\
**Post date:** [November 10, 2022, 1:56pm UTC](https://forum.xojo.com/t/urlconnection-used-for-rest-api/73123/1 "2022-11-10T13:56:11Z")

</div>

Hello,

I tried to use a REST-API to add forex chart data in a sqlite database. The API-Call is in a method of a Window, the URLConnection (Socket) is a control of the same window.

The REST-API-Call works fine, but I would like to use many calls in series, like this:

RESTdataRequest(“EUR”,“USD”,“15min”)  
RESTdataRequest(“AUD”,“USD”,“15min”)  
RESTdataRequest(“USD”,“CHF”,“15min”)

When firing an API-Call it takes time until content is received (contentReceived method of the URLConnection), but (I think) XOJO still is (may be) firing the next RESTdataRequest. Nevertheless an error occurs:

“An exeption of class UnsupportedOperationExeption was not handled. The application must shut down. Exception Message: A request is already in progress.”

What is the best way to avoid this error message?  
What mechanism should I use?  
Do you have code examples?

Kind regards from Germany,  
Clemens

---

<div class="post-metadata">

**Author:** ![Wayne\_Golding](https://forum.xojo.com/user_avatar/forum.xojo.com/wayne_golding/32/165_2.png) [@Wayne\_Golding](https://forum.xojo.com/u/Wayne_Golding)\
**Post date:** [November 10, 2022, 5:40pm UTC](https://forum.xojo.com/t/urlconnection-used-for-rest-api/73123/2 "2022-11-10T17:40:14Z")

</div>

I assume that you are calling the next request method from within the datareceived event handler.

You’ll need to decouple that call by using for example `Timer.CallLater(0, AddressOf RESTdataRequest, Argument)`. Argument needs to be a single variant, so you’ll need to either create a class to hold your 3 arguments or use a dictionary and cast it as a variant.

Another option would be to add a method that calls the request and a property that records the index of the request queue then you could use `Timer.CallLater(0, AddressOf Method)` which would invoke the next request.

HTH  
Wayne

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [November 10, 2022, 6:19pm UTC](https://forum.xojo.com/t/urlconnection-used-for-rest-api/73123/3 "2022-11-10T18:19:50Z")

</div>

You should use one instance of `URLConnection` per request. In the past re-using a `URLConnection` caused unexpected behaviors. Subclassing `URLConnection` will help you significantly.

---

<div class="post-metadata">

**Author:** ![Clemens\_Dähne](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@Clemens\_Dähne](https://forum.xojo.com/u/Clemens_D%C3%A4hne)\
**Post date:** [November 11, 2022, 4:02pm UTC](https://forum.xojo.com/t/urlconnection-used-for-rest-api/73123/4 "2022-11-11T16:02:20Z")

</div>

> [@Tim\_Parnell](#):
>
> `n` will help you significantly.

Thanks Tim, do you can provide a snippet? How to create subclassing an use it?

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [November 11, 2022, 10:02pm UTC](https://forum.xojo.com/t/urlconnection-used-for-rest-api/73123/5 "2022-11-11T22:02:03Z")

</div>

[Building REST API communications is a part of my business](https://xojoconsulting.com), so I don’t offer my libraries and tools for free on the forum. I am happy to offer design advice, but actual implementations are kind of what I do to pay for insulin.

Subclassing can help in many ways. If the remote API requires authentication signatures, you can build that into your class. A subclass can also help centralize error handling. [The documentation will tell you about subclassing here](https://documentation.xojo.com/getting_started/object-oriented_programming/oop_design_concepts.html#inheritance-subclassing)

As individual answers to your initial questions:

> [@Clemens\_Dähne](#):
>
> What is the best way to avoid this error message?

Use one `URLConnection` per request.

> [@Clemens\_Dähne](#):
>
> What mechanism should I use?

An array to hold requests in progress. Best practice would be to clean up the completed requests when they finish.

* * *

If you get stuck again specific questions like the ones you have asked are great! If you have an API you’d like to interact with and just want it built and done, [I do have availability for new consulting projects](https://xojoconsulting.com).

---

<div class="post-metadata">

**Author:** ![Clemens\_Dähne](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@Clemens\_Dähne](https://forum.xojo.com/u/Clemens_D%C3%A4hne)\
**Post date:** [November 14, 2022, 9:35am UTC](https://forum.xojo.com/t/urlconnection-used-for-rest-api/73123/6 "2022-11-14T09:35:31Z")

</div>

Thanks, you tip was great, now it works fine (without an array).
