Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Unanswered

Web Services from Code units: Tailor responses and status codes

(1) ShareShare
ReportReport
Posted on by 21
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 12,475 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,419 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,492 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans