web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Web Services from Code units: Tailor responses and status codes

(2) ShareShare
ReportReport
Posted on by 5
Hello, I am working on a code unit I have exposed as a web service.
 
I was able to successfully publish the and expose the codeunit from the Web Services on the page. However, I am struggling with the following:
Passing specific status codes rather than the default 200 OK e.g. 403 forbidden.
Formatting the Value of the return payload to a JSON object or Array rather than a JSON string.
Currently, regardless of the procedure being successful or not, when the web service is consumed, it'll return a 200 ok status code. I'd want the flexibility to return different status codes dependent on the functions called.
 
Let's say we fail to find a use or some data within a payload is invalid. Hence, rather than returning a 200 OK, it would return a 400 status code. A screen shot of the specific issue I am talking about is below:
 
 
 
A service fails to create a User, but the status code is a 200 OK.
 
Additionally, the value part of the response body is a JSON string rather than a JSON object or array. How can I change my published code unit as web service to implement the above two features?
 
Any tips or pointers is greatly appreciated
I have the same question (0)
  • Gerardo Rentería García Profile Picture
    25,157 Most Valuable Professional on at
  • Suggested answer
    DAnny3211 Profile Picture
    11,397 on at

    Hi there,

    Great question! When exposing a codeunit as a web service in Business Central, you're working within the constraints of the OData or SOAP protocols, which unfortunately don't provide native support for custom HTTP status codes like 403 or 400.

    Here's what you can do:

    1. Custom Status Handling via Response Payload

    Since you can't directly change the HTTP status code, a common workaround is to include a custom status field in your response payload. For example:ù

     

    procedure GetResponse(): Text var Response: JsonObject; begin Response.Add('status', 'error'); Response.Add('code', 403); Response.Add('message', 'User not found'); exit(Response.ToString()); end;

     

    This way, the client consuming the service can interpret the status from the payload rather than relying on the HTTP status code.

    2. Return JSON Object or Array Instead of String

    To return a proper JSON object or array, make sure you're using JsonObject or JsonArray and not converting it to a string unless necessary. If you're using ToString() to return the payload, ensure the consumer parses it correctly as JSON.

    Alternatively, if you're using OData, consider exposing a page or query instead of a codeunit, which gives more control over structured data formats.

    3. Advanced Option: Use Azure Function or API Management

    If you need full control over HTTP status codes and headers, consider wrapping your Business Central logic in an Azure Function or API Management layer. This allows you to call your codeunit internally and expose a more flexible API externally.


    Let me know if you'd like help with sample AL code or setting up a wrapper service.

    If this answer was helpful, please consider marking it as accepted.
    Best regards,
    Daniele

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,143

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,694 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans