Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to send response i...
Finance forum
Suggested answer

How to send response in Json from D365FO for RestAPI

Posted on by 87
Hi Team,
 
I have to send response in json like below
{
  "statusCode": "200", “resultStatus”:success
}
 
For that i am using below contract class 
[DataContract]
class ERPDataContract
{
    Str statusCode;
   
    [DataMember("statusCode")]
    public str parmstatusCode(Str _statusCode = statusCode)
    {
        statusCode= _statusCode;
        return statusCode;
    }
Below is my service class
Class ERPDataService
{
  public str process()
  {
    str jsonString = ("statusCode: 200, resultStatus:success");
    ERPDataContract contract = new ERPDataContract();

   contract.parmStatusCode(jsonString);
   jsonString = FormJsonSerializer::serializeClass(contract);
    info(jsonString);
   return jsonString;
  }
}
Below info i am getting.
{"statusCode": "statusCode: 200, resultStatus:success"}
 
Please let me know how to send response like
{
  "statusCode": "200", “resultStatus”:success
}
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 228,388 Most Valuable Professional on at
    How to send response in Json from D365FO for RestAPI
    200 is an HTTP status code meaning that the request was successful. It's already returned by your custom service, therefore duplicating this information inside the response body is useless. If they really require that, I would let them know that it's not a good design.
     
    But of course that is can be done if they insist. Don't bother trying to create the JSON string by yourself; F&O will do it for you. Simply return the contract object from your service method:
    public ERPDataContract process()
    {
        ERPDataContract contract = new ERPDataContract();
    
        contract.parmStatusCode(200);
        contract.parmResultStatus('success');
    
        return contract;
    }
    The contract class needs two properties you want to return: statusCode and resultStatus.
     
    (Moved from Dynamics AX 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

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,480 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,388 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans