Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

How to get the JSON string from a custom web service ?

Posted on by 362
Hi,
 
Newbie in API custom webservice thing. I created my custom web service, contain these classes : 
1. class TestAPIRequest, contain the data contract attribute
2. class TestAPIResponse, contain response message and success or not
3. class TestAPIService, contain the logic based on the request and return the response.
 
As per my understanding, in normal case will be something like this (for illustration only) :
 
 
1. class TestAPIRequest          
[DataContractAttribute]class TestAPIRequest{          private str     dataAreaId;          private str     journalNum;          [DataMember(/DataAreaId/)]          public str parmDataAreaId(str _value = dataAreaId)          {                  if (!prmIsDefault(_value))                  {                         dataAreaId = _value;                  }        return dataAreaId;          }          [DataMember(/JournalNumber/)]          public str parmJournalNumber(str _value = journalNumber)          {                   if (!prmIsDefault(_value))                   {                          journalNumber= _value;                   }        return journalNumber;           }}
 
2. class TestAPIResponse
[DataContractAttribute]class TestAPIResponse{          private boolean success;          private str     message;           [DataMember(/Success/)]          public boolean parmSuccess(boolean _value = success)          {                  if (!prmIsDefault(_value))                  {                         success = _value;                  }                  return success;           }    [DataMember(/Message/)]           public str parmMessage(str _value = message)          {                   if (!prmIsDefault(_value))                   {                          message = _value;                   }                   return message;            }}
 
 
3. class TestAPIService
public class TestAPIService{    public TestAPIResponse postJournal(TestAPIRequest  _request)    {       try {                    var response = new TestAPIResponse();            changecompany(_request.parmDataAreaId())            {                 .... code....            }            response.parmMessage(/Posted/);            response.parmSuccess(true);       }       catch {              response.parmMessage(/Error/);              response.parmSuccess(false);       }       return response;     }}
Then I've created the ServiceGroup, Services which then available to be called from Postman. In Postman, basically I will put JSON string in the Body, something like:
{    /_request/: {                /DataAreaId/: /TEST/,                /JournalNumber/ : /JN000730/           }}
Question is: if I want to retrieve the request JSON string, when the service class is called, how am I gonna do that ? The intention is I want to keep the JSON string as a string in one of my table for history.
May I have a guide here. What need to be done ? how ? and where to put ?
 
Thanks
 
 
  • Teevo Profile Picture
    Teevo 362 on at
    How to get the JSON string from a custom web service ?
    Seems verified now. Thanks.
     
  • Layan Jwei Profile Picture
    Layan Jwei 7,347 Super User 2024 Season 2 on at
    How to get the JSON string from a custom web service ?
    Hi Teevo,
     
    Is there anything you would like to verify other than the serialize code? If yes, then Please specify the answers that you would like to verify and we'll verify them for you.
     
    Maybe Specify the person and the timing of the answer.
     
     
    Thanks,
    Layan Jweihan
  • Teevo Profile Picture
    Teevo 362 on at
    How to get the JSON string from a custom web service ?
    Hi Layan,
     
    Seems I cannot mark it, it always freeze, just keep processing without end. Can you help to mark it ?
    Thanks.
  • Layan Jwei Profile Picture
    Layan Jwei 7,347 Super User 2024 Season 2 on at
    How to get the JSON string from a custom web service ?
    Hi Teevo,

    If your question is answered then please mark all answers that helped.

    I'm not sure I got your question correctly. but if you want to test it using runnable class by passing the json string instead of filling the contract class, then a workaround can be that you define the json string, then deserialize it back to contract, then pass it to the custom service.

    I mean:
    str json =  {"_request":{"DataAreaId":"Test}}
    TestAPIRequest apiRequest = FormJsonSerializer::deserializeObject(classNum(TestAPIRequest), json);

    If you have further questions, then it's better to create a new question

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future. You can do this by ticking the box "Does this answer your question"
  • Teevo Profile Picture
    Teevo 362 on at
    How to get the JSON string from a custom web service ?
    Hi Layan,
     
    Seems work. May I know one thing, though. It's a bit not related, but yes if we saw it from testing POV. How to test this without Postman ? I mean I don't have the configuration such as my TenantID, etc, so can I run my Service class from URL, with addition of the JSON string ?
    Because currently I'm testing it from Runnable class, and in that runnable class is by passing the parameter one by one according to the contract class, then in that runnable class call my service class. It is not from a real JSON String.
     
    Sorry about the previous thread. I haven't continue it yet, and have to jump to other more urgent like this one. 
     
    Thanks.
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    How to get the JSON string from a custom web service ?
    I believe I already answered the question whether it's possible. Logging the original request in your service class isn't possible, serializing the object back to JSON is possible.
  • Verified answer
    Layan Jwei Profile Picture
    Layan Jwei 7,347 Super User 2024 Season 2 on at
    How to get the JSON string from a custom web service ?
    Hi Teevo,

    Yes your request class, i just called it classTest. 

    So in your case, if you will put the code inside TestAPIService, you will use this:
     str jsonString = FormJsonSerializer::serializeClass(_request);  -- let us know if it works :)

    please don't forget to verify answers that helped you to help others, I can see you have previous questions that you didn't close like this one:

    https://community.dynamics.com/forums/thread/details/?threadid=4cd9d55a-e499-ee11-be37-000d3a4fe8f7
    So please go through them

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future. You can do this by ticking the box "Does this answer your question"
  • Teevo Profile Picture
    Teevo 362 on at
    How to get the JSON string from a custom web service ?
    Hi Layan,
     
    But what object is this classTest() ? should I put my Request class ?
     
    Since if I take a look at the method, it is also just mentioned that it receive object. Not sure what kind of object.
     
     public static str serializeClass(Object _object)
        {
            FormJsonSerializer serializer;
    
            if (_object == null)
            {
                return 'null';
            }
    
            serializer = new FormJsonSerializer();
            serializer.serializeObject(_object);
    
            return serializer.json();
        }
     
    Can you help to applied it into my example so I can understand more on this.
    Thanks.
     
     
     
  • Suggested answer
    Layan Jwei Profile Picture
    Layan Jwei 7,347 Super User 2024 Season 2 on at
    How to get the JSON string from a custom web service ?
    Hi Teevo,

    In general, here's how you can get a JSON out of your request contract:
     
    class classTest = new classTest();
    str jsonString = FormJsonSerializer::serializeClass(classTest);

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future. You can do this by ticking the box "Does this answer your question"
  • Teevo Profile Picture
    Teevo 362 on at
    How to get the JSON string from a custom web service ?
    Hi Martin,
     
    Ok, I will take note on your advice about this is not a good idea. But is it possible though ? And how to achieve that if it is for additional knowledge ? 
    Thank you.

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans