Error BAD_REQUEST idempotency_key

Hi,

when making this call from my custom app on a mac machine this call works. The same custom app running on windows 11 provides this error which i do not understand because i am not including any idempotency_key field. Not sure why I am getting this error. Again this in on windows 11

Below are my logs… I am working on the sandbox.

2024-05-08 17:27:27: Request Endpoint - https://connect.squareupsandbox.com/v2/devices/codes/X302DC34SD90
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 GetDeviceCode or GetTerminalCheckout on windows not on mac. It looks like the first call to CreateDeviceCode and CreateTerminalCheckout work fine or maybe because those calls are done first that the second calls fail with the error above.

[quote=“SDCo AARP, post:1, topic:15564”]
Hi,

when making this call from my custom app on a mac machine this call works. The same custom app running on windows 11 provides this error which i do not understand because i am not including any idempotency_key field. Not sure why I am getting this error. Again this in on windows 11

Below are my logs… I am working on the sandbox.

2024-05-08 17:27:27: Request Endpoint - https://connect.squareupsandbox.com/v2/devices/codes/X302DC34SD90
2024-05-08 17:27:27: Request Response - {"errors": [{"code": "BAD_REQUEST","detai

[/quote]
Hello, @SDCo

It seems like your issue is related to an idempotency_key error when making an API call from your custom app on Windows 11, despite the fact that you’re not explicitly including an idempotency_key field. The idempotency_key is used to ensure that a request is not processed more than once, which can be particularly useful for preventing duplicate transactions in financial applications.

Here are a few steps you can take to troubleshoot this issue:

Check Default Headers: Ensure that your app is not automatically setting an idempotency_key in the headers when making the call on Windows 11.
Compare Environments: Verify that the environment variables and configurations are the same on both your Mac and Windows machines.
Examine the API Documentation: Review the API documentation for the service you’re using to understand how it handles idempotency keys and if they are required or automatically generated.
Inspect the Error Message: The error message seems to be incomplete. Check the full error response for more details on what might be causing the BAD_REQUEST error.
Sandbox Environment: Since you’re working in a sandbox environment, confirm that the sandbox settings are consistent across different operating systems and that there aren’t any OS-specific limitations or behaviors.

I hope my suggestion is helpful for you.

Best Regard,
patrick521

There may be differences in how the custom app is handling the API request on Windows versus Mac. It’s possible that there is a bug or configuration issue in the Windows version of the app that is causing the ‘idempotency_key’ field to be included or expected. What’s the snippet of code your getting this error with? :slightly_smiling_face:

Hi @Bryan-Square ,

It is the same code (Xojo allows you to use the same code and compile it for Mac, Linux and Windows)

Here is the code to GetDeviceCode that i call AFTER CreateDeviceCode api and i get the error response above. This also happens after I call GetTerminalCheckout on windows only…

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 my CreateDeviceCode snippet that gets call before GetDeviceCode

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

The error message you’re receiving when calling GetDeviceCode or GetTerminalCheckout on Windows is indicating that an idempotency_key field is being unexpectedly included or required in the request, even though your code does not seem to explicitly include it for those calls.

Given that the same code works on a Mac but not on Windows, it suggests that the issue is not with the code itself but possibly with the environment or the way the HTTP requests are being executed on Windows.

Here are some steps you can take to troubleshoot and resolve the issue:

  1. Check for Environmental Differences: Ensure that the environment on Windows is the same as on Mac. This includes the version of Xojo you’re using, any libraries or plugins, and network configurations.
  2. Inspect HTTP Requests: Use a tool like Wireshark or Fiddler to capture the HTTP requests being sent from Windows and compare them to the requests from Mac. Look for any discrepancies, especially related to headers or body content.
  3. Review Error Messages: The error message suggests that the issue is with the idempotency_key field. Double-check that this field is not being set or cached somewhere in your code or by any middleware.
  4. Check Xojo Network Classes: If you’re using Xojo’s built-in network classes, ensure they are behaving consistently across both platforms. There might be platform-specific bugs or behaviors.
  5. Update Xojo: Make sure you’re using the latest version of Xojo, as there could be bug fixes that resolve platform-specific issues.
  6. Check Square API Documentation: Review the Square API documentation to ensure that your implementation matches their requirements and that there haven’t been any recent changes that might affect your calls.
  7. Reach Out to Xojo Community: The Xojo community forums can be a great resource. Other developers may have encountered similar issues and can offer solutions.
  8. Contact Square Support: Since you’re working with Square APIs, reaching out to their developer support might provide insights or known issues related to platform differences.
  9. Test with Minimal Code: Create a minimal test case that only makes the GetDeviceCode or GetTerminalCheckout call without any other logic. This can help isolate the issue.
  10. Check for Hidden Characters: Sometimes, copying and pasting code can introduce hidden characters that might cause issues. Retype the relevant sections manually to eliminate this possibility.
  11. Try a Different HTTP Client: If you’re using a particular HTTP client library, try switching to a different one to see if the problem persists. This can help determine if the issue is with the library. :slightly_smiling_face:

Thank you guys, I found the issue. It looks like Xojo URLConnection object is not clearing the body from the previous call. A sticky body? What i did to fix this was to set the body to nothing…

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

Here is the full code…

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"

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

    Self.Send("GET", url )
  Catch err as RuntimeException
    MessageBox("DeviceConnector CreateDeviceCode Error: " + err.Message)
  End
End Sub

Again, thank you for all your help.

Thanks for sharing your findings. :slightly_smiling_face: