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

Community site session details

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

How to pass an object in the request body of api? x++

(0) ShareShare
ReportReport
Posted on by 1,552

So the the api was accepting one string parameter so i did this

System.Text.Encoding encoding  = System.Text.Encoding::UTF8;
        System.Byte[] byteArray  = encoding.GetBytes(_data); //_data is string
        _request.set_ContentLength(byteArray.get_Length());
        using(System.IO.Stream dataStream = _request.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.get_Length());
        }

Now the api accepts two paramters in the body

So i made an object class that contains two fields str dataString and int dataId and i need to pass these two values to the body

i think i should repalce getBytes(_data) with getBytes(object.dataString) but how can i pass dataId as well?

So in postman it works if i write in the body

{

  "dataString": kfsifks,

  "dataId":1

}

I have the same question (0)
  • Martin Dráb Profile Picture
    236,320 Most Valuable Professional on at
    RE: How to pass an object in the request body of api? x++

    API stands for "Application Programming Interface", therefore it's an extremely wide term. You should be much more specific.

    Your code suggests that your calling a web service from X++, using WebRequest or something (unfortunately you didn't mention what object you have in _request variable).

    What kind of data does the web service expects? A JSON object, maybe?

  • junior AX Profile Picture
    1,552 on at
    RE: How to pass an object in the request body of api? x++

    Hi Martin,

    yes i'm calling a web service. _ request is of type System.Net.WebRequest and the contentType is application/json

    {

      "dataString": kfsifks,

      "dataId":1

    }

  • Verified answer
    Martin Dráb Profile Picture
    236,320 Most Valuable Professional on at
    RE: How to pass an object in the request body of api? x++

    You could build a JSON string by yourself, but the better approach is using a JSON serializer. The serializer will take an object and convert it to corresponding JSON string.

    You said you already created a class for your data. Implement it as a data contract, use FormJsonSerializer::serializeClass() and check if the result matches what you need.

  • junior AX Profile Picture
    1,552 on at
    RE: How to pass an object in the request body of api? x++

    Thanks martin,

    quick question isn't a web service considered an api?

  • Martin Dráb Profile Picture
    236,320 Most Valuable Professional on at
    RE: How to pass an object in the request body of api? x++

    API is an extremely wide term. While web services are a kind of API, if you say an API, it's not clear that you're talking about web services. You might be talking about a hundred different things.

    Even if you say that you mean web services, it's not clear which type. For example, you might be talking about SOAP.

    If we don't know what you're talking about, it's difficult for us to help you, therefore giving us enough information is in your own interest.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,047

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 885 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 592 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans