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 I can receive IRestResponse (RestSharp) in x++

(0) ShareShare
ReportReport
Posted on by 332

Hello Experts

I am using restSharp to send a requset to external API.

So first thing I createed two class libraries using RestSharp on for token and the other for send SO 

Now i need to receive the IRestResponse  in a object in x to check the status code 

in the class library I am using 

var client = new RestClient(_uri);
client.Timeout = -1;
string token = "Bearer "   _token;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", token);
request.AddHeader("Content-Type", "application/json");
var jsonConverted = JsonConvert.SerializeObject(_json);
request.AddParameter("application/json", jsonConverted, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
        
return response.Content;

but the content doesn't has the status code , the status code in the (IRestResponse)

IRestResponse response = client.Execute(request);

Thanks

I have the same question (0)
  • Martin Dráb Profile Picture
    235,934 Most Valuable Professional on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    I'm glad to be of help.

    By the way, don't forget to add the assembly and the AOT reference to source control.

  • Ahmed Osama Ibrahim Profile Picture
    332 on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    Realy you always save my time

    Thanks

  • Ahmed Osama Ibrahim Profile Picture
    332 on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    First I found RestSharp  in my model bin folder

    I deleted it , and found the dll of th RestSharp  in the

    C:\Users\Administrator\source\repos\Integration\packages\RestSharp.108.0.1\lib\net6.0

    RestSharp.dll

    I copied it to my model in the Bin folder

    Now I can see the RestResponse class

    Thanks Martin

  • Verified answer
    Martin Dráb Profile Picture
    235,934 Most Valuable Professional on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    Please look into the bin folder of your package to verify that RestSharp isn't there.

    Then extract the NuGet package, find the right DLL (there may be builds for different processor architecture) and put it to the bin folder of your package. Then add a reference in AOT. If there are assemblies that RestSharp depends on and they aren't available in F&O, add them too.

  • Ahmed Osama Ibrahim Profile Picture
    332 on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    Thanks Martin

    First

    01- I created .net class library in a project named NETIntegration.

    02 - Install RestSharp from Nuget (latest version)

    3 - Add method  to insert data in other system 

    The code in the class library is 

    public RestResponse sendInvoice(string _uri,string _token ,string _json)
    {
          string token = "Bearer "   _token;
                request.AddHeader("Authorization", token);
                request.AddHeader("Content-Type", "application/json");
                var jsonConverted = JsonConvert.SerializeObject(_json);
                request.AddParameter("application/json", jsonConverted, ParameterType.RequestBody);
                RestResponse response = client.Execute(request);
    
                return response;
    }

    04 - I tested that with consol app and working fine in cosnol app , I can chk on  the status code and can catch the content of the reurn 

    05 - In x project -- Reference -- add reference and chked the NetIntegration project 

    06- Created a new class

    07- add using to the new reference using NETIntegration;

    08- Create new instance of the class that in the  (NETIntegration) named test

    09- use the method that in the test 

    ( RestResponse respons = test.sendInvoice(paramater))

    The only coe in the x is that   

     RestResponse respons = test.sendInvoice(paramater)

  • Martin Dráb Profile Picture
    235,934 Most Valuable Professional on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    Trying to add a project reference is a bad approach. You don't have a project with source code if RestSharp, do you?

    Please start from the beginning and tell us what you did. Show us your code, both the code in the class library (C#, I guess) and your X++ code. Tell us how you added a reference to RestSharp in your class library (NuGet, for instance) and you added a reference to your class library in F&O (a project reference in a single solution?).

  • Ahmed Osama Ibrahim Profile Picture
    332 on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    Thanks Marting

    You said : must have a reference to the assembly containing the class 

    How I add RestSharp reference to x++ project

    ress.png

    So I need to refenece my project to the assembly , How i can add RestSharp to project reference

    Thanks

  • Martin Dráb Profile Picture
    235,934 Most Valuable Professional on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    Aha, so the question is how to declare a variable of that type.

    You must have a reference to the assembly containing the class and then you either need to use the fully-qualified name:

    RestSharp.RestResponse response;

    or you can utilize the using statement on the top of the file, and then refer just to the class name (without its namespace):

    using RestSharp;
    
    ...
    
    RestResponse response;

    But now your code is completely different. We discussed Execute() method of RestClient class, while your code doesn't call Execute() at all. You're calling getSendInvoice() method of a class unknown to us.

  • Ahmed Osama Ibrahim Profile Picture
    332 on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    Thanks Martin

    Now I  am using last version of RestSharp

                var client = new RestClient(_uri);
                var request = new RestRequest(_json, Method.Post);
    
    
    
    
                string token = "Bearer "   _token;
                request.AddHeader("Authorization", token);
                request.AddHeader("Content-Type", "application/json");
                var jsonConverted = JsonConvert.SerializeObject(_json);
                request.AddParameter("application/json", jsonConverted, ParameterType.RequestBody);
                RestResponse response = client.Execute(request);
    
                return response;

    Same Q how i can reseive RestResponse in x , how I can declare the RestResponse  in X

    11111111122.png

    the meaning of content dosn't incloude the status code is , the content is contain the json body in the response (not contain the status code)

    How I can use using RestSharp in x  

  • Suggested answer
    Martin Dráb Profile Picture
    235,934 Most Valuable Professional on at
    RE: How I can receive IRestResponse (RestSharp) in x++

    What exactly do you mean by "the content doesn't has the status code"?

    Which version of RestSharp are you using? IRestResponse is deprecated and according to source code, Execute() now returns RestResponse, which doesn't implement any interface and therefore it's not compatible with IRestReponse).

    So your problem doesn't seem to be related to X++.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 777 User Group Leader

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 602 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans