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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Custom Service - How to return HTTP 400 Bad Request?

(0) ShareShare
ReportReport
Posted on by 15
Hi everyone,

I'm building a Custom Service in D365 F&O (X++) that is called from an external website. The service works fine, but I noticed that it always returns HTTP 200 OK regardless of the outcome — even when my business logic fails (e.g. record not found, etc.).

Currently I'm returning a custom response contract with a Success (boolean) and Message (string) field, and checking Success on the client side. This works but ideally I'd like to return proper HTTP status codes like 400 Bad Request for business validation failures.

Is there any way to control the HTTP response status code from within a D365 JSON Custom Service?

Thank you
Categories:
I have the same question (0)
  • Suggested answer
    Deepak Agarwal Profile Picture
    8,728 on at
    If you are using your custom service, you need to return error response when there is an error, not default success. Use this URL  HTTP status codes to refer what code can be used in different error conditions. 
  • Verified answer
    Martin Dráb Profile Picture
    239,444 Most Valuable Professional on at
    I disagree that you get 200 when a custom service throws an unhandled exception. You get 500 in such a case, as far as I know. Are you sure that you threw an exception?
     
    Anyway, you don't control the error codes returned by F&O; all you can do is letting the operation complete successfully or throw an exception. What you could do is putting something like Azure API Management in front of the custom service, checking the response (e.g. looking for a particular infolog message) and change the status code of the response.
  • Raed Salah Bzour Profile Picture
    15 on at
    Hi Deepak,
    thank you for your response, I am pretty familiar with these status codes, what I wanted is to change the status code based on the response
     
  • Raed Salah Bzour Profile Picture
    15 on at
    Hi Martin,
    Of course, it returns 500 server error when I throw an exception, However I want to control the return status code like when the record is empty to return 404 Not Found status code also 400 for bad request if the validation is not met
    I was wondering if there any built-in approach to control the return status code
     
    Thanks
  • Deepak Agarwal Profile Picture
    8,728 on at
    There is no built in approach in X++ to handle different status, you need to write custom code for that. I would suggest to identify all the use case and related status code and return them accordingly when different error occured. I used the same approach recently. 
  • Raed Salah Bzour Profile Picture
    15 on at
    Hi Deepak,
    I understand your point and I almost use the same approach by returning success and message, I can return the status code as well, but what I want it to have the full control on the response.
     
    I will stick with Martin's solution using Azure API Management.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 608 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 564

#3
CP04-islander Profile Picture

CP04-islander 396

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans