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...
Unanswered

Web Services from Code units: Tailor responses and status codes

(1) ShareShare
ReportReport
Posted on by 1
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
  • gdrenteria Profile Picture
    gdrenteria 14,573 Most Valuable Professional on at

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans