Daniel Erling
Daniel Erling

Reputation: 21

Business Central 365 (SaaS, Version 22.3) API-Encoding-Problem

I have the following problem: We have implemented a page (incl. table) of type "API" in our Business Central 365 environment (SaaS), which should receive posting information from an external financial accounting. This data is then further processed in a separate process. It is now the case that posting texts (freely fillable by the clerk) are also part of this posting information. Since we are in the German language area, it can happen that German umlauts are used in these posting texts. If we now want to use a simple Powershell script to transfer this data from the source system to the target system via an API call, the umlauts are not resolved correctly. Thus a "ü" becomes a "\u00fc" in the target column. We use (for test purposes) the following Powershell script:

$Headers = @{
    "Content-type"              = "application/json; charset=utf-8"
    "Content-Transfer-Encoding" = "binary"
    "Authorization"             = "Bearer " + $BearerToken
    "Company"                   = "xxxxxxxx-xxxx-xxx-xxxx-xxxxxxxxxxxx"
}

$body = @{
    "requests" = @(
        @{
            "method"  = "POST"
            "id"      = "REQ1"
            "url"     = "companies(xxxxxxxx-xxxx-xxx-xxxx-xxxxxxxxxxxx)/transactions"
            "headers" = @{
                "Content-Type"              = "application/json; charset=utf-8"
                "Content-Transfer-Encoding" = "binary"
            }
            "body"    = @{
                "ABRECHNUNGNR" = "6661"
                "BUCHUNGSTEXT" = "Büromöbel"
            }
        }
    )
}

$bodyText = ($body | ConvertTo-Json -Depth 100)

$url = 'https://api.businesscentral.dynamics.com/v2.0/Environment/api/publisher/transaction/v1.0/$batch'

$response = Invoke-RestMethod -Method Post -Uri $url -Headers $Headers -Body $bodyText

After the transfer to Business Central 365 (SaaS) we get the following value for "BUCHUNGSTEXT" in the target table:

enter image description here

We would have expected the transferred "BUCHUNGSTEXT" to appear in Business Central as we specified it.

We are urgently looking for a possible solution. Thank you very much for your support.

UPDATE 2023-08-26

We have solved the problem: We found out that it makes a difference in which notation you use for the data attributes. In the API page, the attribute name is "buchungstext". But we have specified the attribute as "BUCHUNGSTEXT" in our request. However, I cannot provide a reason for this. After we specified the attributes in the batch request in the correct notation, the encoding problem disappeared.

Thanks all for your help!

Upvotes: 1

Views: 525

Answers (1)

kaspermoerch
kaspermoerch

Reputation: 16560

TLDR; You have to remove the Content-Type header on REQ1.

I believe you are double encoding your body content.

The Content-Type defined in the headers for the POST to the $batch endpoint will also encode the values in each of the requests which will encode Büromöbel to B\u00fcrom\u00f6bel.

When the actual request is then called the content will be encoded again which means B\u00fcrom\u00f6bel becomes B\u005cu00fcrom\u005cu00f6bel because the backslash is encoded.

Removing the Content-Type header from REQ1 should fix the problem.

Upvotes: 1

Related Questions