# URLConnection Issues in Windows

**URL:** <https://forum.xojo.com/t/urlconnection-issues-in-windows/80259>\
**Category:** Windows\
**Created:** [May 9, 2024, 8:47pm UTC](https://forum.xojo.com/t/urlconnection-issues-in-windows/80259 "2024-05-09T20:47:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![C\_P](https://forum.xojo.com/user_avatar/forum.xojo.com/c_p/32/17577_2.png) [@C\_P](https://forum.xojo.com/u/C_P)\
**Post date:** [May 9, 2024, 8:47pm UTC](https://forum.xojo.com/t/urlconnection-issues-in-windows/80259/1 "2024-05-09T20:47:11Z")

</div>

Hello Team,

I am having an issue with URLConnection and Windows. It works on Mac. I am using the api for square to connect to a terminal.

When making this call from my custom Xojo app on a Mac machine, this call works. No rrrors. The same custom app running on windows 11 provides the error below which i do not understand because i am not including any idempotency\_key field.

Below are my logs…

```auto
2024-05-08 17:27:27: Request Endpoint - https://connect.squareupsandbox.com/v2/devices/codes/X402DC55SD90
2024-05-08 17:27:27: Request Response - {"errors": [{"code": "BAD_REQUEST","detail": "The field named 'idempotency_key' is unrecognized (line 1, character 2)","field": "idempotency_key","category": "INVALID_REQUEST_ERROR"}]}

```

This happens when making a call to the api GetDeviceCode or api GetTerminalCheckout on windows. It looks like the first call to CreateDeviceCode and CreateTerminalCheckout work fine. Maybe since those previous calls are done first everything works but then the second calls fail with the error above because…?

The sub below fails when calling it second, but it seems to work if i call it first.

```auto
Sub GetDeviceCode(deviceCodeId As String)
  'GetDeviceCode API Call
  'GET https://connect.squareupsandbox.com/v2/devices/codes/{DEVICE_CODE_ID}
  'Square-Version: 2024-04-17
  'Authorization: Bearer {TOKEN}
  'Content-Type: application/json
    
  Try
    Var url As String = App.GetAPIURL() + "/v2/devices/codes/" + deviceCodeId
    
    Self.mDeviceCodeId = deviceCodeId
    
    Self.RequestHeader("Square-Version") = "2024-04-17"
    Self.RequestHeader("Authorization") = "Bearer " + Self.mAccessToken
    Self.RequestHeader("Content-Type") = "application/json"
    Self.Send("GET", url )
  Catch err as RuntimeException
    MessageBox("DeviceConnector CreateDeviceCode Error: " + err.Message)
  End
End Sub

```

This is the first call

```auto
Sub CreateDeviceCode(deviceName As String, idempotencyKey As String, locationId As String)
  'POST https://connect.squareupsandbox.com/v2/devices/codes //FOR TESTING
  'POST https://connect.squareup.com/v2/devices/codes // FOR PROD
  'Square-Version: 2024-04-17
  'Authorization: Bearer {TOKEN}
  'Content-Type: application/json
  '
  '{
  ' "idempotency_key": "0a86b4e1-1b29-4332-98c6-4703c1afe33e",
  ' "device_code": {
  ' "name": "MyApp",
  ' "product_type": "TERMINAL_API",
  ' "location_id": "LOCATION_HERE"
  ' }
  '}
    
  Try
    Var url As String = App.GetAPIURL() + "/v2/devices/codes"
    Var body As New JSONItem
    Var deviceCode As New JSONItem
    
    deviceCode.value("name") = deviceName
    deviceCode.value("product_type") = "TERMINAL_API"
    
    If Not locationId.IsEmpty Then
      deviceCode.value("location_id") = locationId //Square will use the default location
    End If
    
    body.value("idempotency_key") = idempotencyKey
    body.value("device_code") = deviceCode
    
    Self.RequestHeader("Square-Version") = "2024-04-17"
    Self.RequestHeader("Authorization") = "Bearer " + Self.mAccessToken
    Self.RequestHeader("Content-Type") = "application/json"
    Self.SetRequestContent(body.ToString, "application/json")
    Self.Send("POST", url)
  Catch err as RuntimeException
    MessageBox("DeviceConnector CreateDeviceCode Error: " + err.Message)
  End
End Sub

```

To me as you can see the `idempotency_key` is added to the first call CreateDeviceCode in the body not header and because of this somehow the second call GetDeviceCode fails. Sticky headers or something weird happening or something is added to the second call were it does not need to be there. Again this works on the mac with no issues. Any guidance will be great.

TIA

---

<div class="post-metadata">

**Author:** ![Andrew\_Lambert](https://forum.xojo.com/user_avatar/forum.xojo.com/andrew_lambert/32/215_2.png) [@Andrew\_Lambert](https://forum.xojo.com/u/Andrew_Lambert)\
**Post date:** [May 9, 2024, 10:08pm UTC](https://forum.xojo.com/t/urlconnection-issues-in-windows/80259/2 "2024-05-09T22:08:27Z")

</div>

This doesn’t look like sticky headers, but a sticky body. The GET request works when it comes first because no body is set yet. It fails when it comes second because the previous body is being sent. Try explicitly setting the body to empty for GET requests, or creating a new URLConnection for each request.

---

<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:** [May 9, 2024, 10:38pm UTC](https://forum.xojo.com/t/urlconnection-issues-in-windows/80259/3 "2024-05-09T22:38:23Z")

</div>

I always create a new urlconnection for each request so I start clean each time.

---

<div class="post-metadata">

**Author:** ![C\_P](https://forum.xojo.com/user_avatar/forum.xojo.com/c_p/32/17577_2.png) [@C\_P](https://forum.xojo.com/u/C_P)\
**Post date:** [May 10, 2024, 3:32am UTC](https://forum.xojo.com/t/urlconnection-issues-in-windows/80259/4 "2024-05-10T03:32:08Z")

</div>

I just did a simple hack…

`Self.SetRequestContent("", "application/json") //this will stop issues in MS Windows with sticky body`

I wanted to instanciate a different URLConnection everytime i had a call. But i was wondering how would i do the contentreceived event. Maybe with an addhandler (delegate)? Not sure. Thank you for your insights!

---

<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:** [May 10, 2024, 5:05am UTC](https://forum.xojo.com/t/urlconnection-issues-in-windows/80259/5 "2024-05-10T05:05:13Z")

</div>

You can subclass the urlconnection & implement the data received event handler.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [May 10, 2024, 12:29pm UTC](https://forum.xojo.com/t/urlconnection-issues-in-windows/80259/6 "2024-05-10T12:29:03Z")

</div>

> [@C\_P](#):
>
> I just did a simple hack…
> 
> `Self.SetRequestContent("", "application/json") //this will stop issues in MS Windows with sticky body`

That’s not a hack, that’s the proper way of reusing the object changing the body of the request (in this case, to nothing).
