Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

How to return 'Error 404' from a Custom Service API

(0) ShareShare
ReportReport
Posted on by 129

Hello Community,

i am developing an API on D365FO to expose data as Custom Service.
As i check no records were found, it should return a 404,
but a test ran from Postman receives a status 200 - ok

My returned JSON is ok, like below:

{
    "Company": "2",
    "InfologText": "Ciclo orçamentário 2022 não é válido.",
    "IntegEnvironment": 0,
    "Integration": 0,
    "ResultSet": []
}

Can anyone help how to change Response Status?

Thanks.

  • Verified answer
    Martin Dráb Profile Picture
    231,880 Most Valuable Professional on at
    RE: How to return 'Error 404' from a Custom Service API

    You can't define HTTP code of a custom service response by yourself. F&O give you only two options: success or failure (when you throw an exception).

    Maybe you could use Azure API Management to analyze the response, drop it and return 404 instead.

    By the way, consider creating another thread for your problem with Postman on DEV VM.

  • Hugo Alves Profile Picture
    129 on at
    RE: How to return 'Error 404' from a Custom Service API

    First of all, thanks Martin,
    but after these thoughts let me improve the question,
    and let´s make it a relevant future reference to this subject...

    Is it possible to either handle exceptions and also Change the Response Status to 404?
    (lets say i want to have control on replied Response JSON, or any other feasiable scenario)

    Or Throwing Error is recommended/possible approach to Reply Error on Custom Service?
    and in this case we have no control on replied JSON.

    Well, since i am not able to test Postman on DEV VM, it will take me some time to discover what error number the unhandled exception would return.

  • Martin Dráb Profile Picture
    231,880 Most Valuable Professional on at
    RE: How to return 'Error 404' from a Custom Service API

    This doesn't look like an exception to me. It seems that your service successfully returned data (such as BudgetCycleName) - therefore it handled the exception and didn't throw any - and it merely included infolog messages in InfoText property. You may want to debug you service to see where the exception got handled.

    It seems that somebody intentionally designed the service not to fail even if an exception occurs.

  • Hugo Alves Profile Picture
    129 on at
    RE: How to return 'Error 404' from a Custom Service API

    i've forced a Throw, but still i get Status 200 on Postman.

    API-Test-Postman.PNG

  • Martin Dráb Profile Picture
    231,880 Most Valuable Professional on at
    RE: How to return 'Error 404' from a Custom Service API

    Why don't you simply throw an exception, if you consider that a failure?

  • Ludwig Reinhard Profile Picture
    Microsoft Employee on at
    RE: D365FO - How to return 'Error 404' from a Custom Service API

    moved to the d365 forum

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... 293,099 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,880 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans