0

I have an issue where I can't quite see which .net encoding method to use for setting fields for the above. Here's my code:

Contact Contact = null;
Contacts Contacts = null;
List<Contact> ContactsList = null;

ContactsList = new List<Contact>();
Contacts = new Contacts();

//no error
Contact.BankAccountDetails = "detew";

//400 Bad Request error
Contact.BankAccountDetails = "&ew";

//I have tried encoding using:

System.Uri.EscapeDataString
System.Security.SecurityElement.Escape
System.Net.WebUtility.HtmlEncode
System.Xml.XmlConvert.EncodeName
System.Web.HttpUtility.HtmlEncode
System.Web.HttpUtility.UrlEncode
System.Web.HttpUtility.HtmlAttributeEncode

//But nothing works.

//This is the passed xml with error:

Xero.NetStandard.OAuth2.Client.ApiException: 'Xero API 400 error calling UpdateContact :{
  "ErrorNumber": 10,
  "Type": "ValidationException",
  "Message": "A validation exception occurred",
  "Elements": [
    {
      "ContactID": "89d59636-6524-47b2-b047-0000000000000",
      "ContactNumber": "Cust_4",
      "AccountNumber": "Cust_4",
      "ContactStatus": "ACTIVE",
      "Name": "cpName",
      "FirstName": "MyCustomerFirstName2",
      "LastName": "MyCustomerLastName1",
      "EmailAddress": "sfgdg@sdfs.lo",
      "SkypeUserName": "",
      "BankAccountDetails": "bkname&amp; bankacname sortcode accnumber bicswift iban",
      "UpdatedDateUTC": "\/Date(1604408524790)\/",
      "ContactGroups": [],
      "IsSupplier": false,
      "IsCustomer": false,
      "ContactPersons": [],
      "HasAttachments": false,
      "HasValidationErrors": true,
      "ValidationErrors": [
        {
          "Message": "The BankAccountDetails field cannot be more than 50 characters long."
        }
      ]
    }
  ]
}'

Converting to UTF8 (How can I transform string to UTF-8 in C#?) as per thread https://community.xero.com/developer/discussion/32311#answer32331 does not work either.

Maybe I need to set the Content-Type to xml but am not sure how it's done using the Xero DSK. Maybe via XeroConfiguration when the connection is made?

Thanks.

MikeG001
  • 53
  • 4
  • You may be using TLS 1.0/1.1 and you need to use TLS 1.2. See : https://developer.xero.com/documentation/oauth2/overview – jdweng Nov 03 '20 at 17:14
  • I'm using TLS 1.2 – MikeG001 Nov 03 '20 at 19:59
  • See following : https://www.example-code.com/csharp/xero_oauth2.asp. The following uses the httpclient in xero and add a header User Agent to the client. Content-Type is also a header that would be added in a similar fashion : https://github.com/XeroAPI/Xero-Net/blob/dcbacf90b27c7196cf4f8073da4b6d690ea24192/Xero.Api/Infrastructure/OAuth/OAuthTokens.cs#L71 – jdweng Nov 04 '20 at 01:11
  • Thanks but I think this isn't using the sdk – MikeG001 Nov 04 '20 at 12:58
  • Does it matter? You are still using the same client and want to add a HTTP header. – jdweng Nov 04 '20 at 13:04
  • There r 2 objects one (XeroConfiguration) is passed to the other (XeroClient) but I can't see any constrructor or method that takes the content type: ``` xConfig.ClientId = strClientId; xConfig.CallbackUri = new Uri(strRedirectUrl); xConfig.Scope = strAccScopes; xConfig.AppName = strAppName; xClient = new XeroClient(xConfig); return xClient.BuildLoginUriPkce(strCodeVerifier); ``` – MikeG001 Nov 04 '20 at 14:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224096/discussion-between-mikeg001-and-jdweng). – MikeG001 Nov 04 '20 at 14:39

1 Answers1

0

The issue wasn't encoding but in my actual code I have a bigger string that I use to set BankAccountDetails and without realising I was setting it with a sligtly longer string whenever I was changing it to add special char's which made it go over the max length for the field. It works fine now. Thanks to all.

MikeG001
  • 53
  • 4