previous arrowprevious arrow
next arrownext arrow
PlayPause
previous arrownext arrow
Shadow

We realize your CAD projects

in AutoCAD, Inventor, Revit and Autodesk Forge Platform

environment

We help you with the realization of your projects.

Blog - CAD-Becker.de

Contributions to AutoCAD and its vertical products.
Tips and tricks, lots of news from the Autodesk world.
2 minutes reading time (448 words)

Autodesk Forge: How to translate the cURL documentation to C #?

Anzeige

Aktuelle Top-Angebote der Telekom, Online-Vorteile, Attraktive Prämien

The Forge Platform documentation is not easy to understand for a pure C # or VB.Net programmer.

Autodesk documented the call using cURL. I would like to explain in my new posting on Forge how cURL can be translated into C #.

...

curl

Originale website
Ein cURL-Beispiel

CURL describes the request (REQUEST) to the web service, here Forge. .Net framework has no function that translates cURL directly.

One way to build a request would be to use RestSharp. RestSharp can be added to the application as a NuGet package.

The following table lists the options (those with the leading minus sign).

 
Options Description RestSharp
-v Method or URI
-x HTTP method type Method.Get or Method.Post
-H Header Several header parameter are possible
-d Parameter data i.e.Client-ID

The following code snippet shows how to build a RestSharp.RestRequest. This requires a RestSharp.

The URL (variable: m_URL) corresponds to the -v option in the cURL. The method of the option -x.

string m_Url = "https://developer.api.autodesk.com/authentication/v1/gettoken";
RestRequest m_Request = new RestRequest(m_Url, Method.POST);} 
The cURL option -H (header) is defined with the RestRequest method AddHeader. This method expects a key and a value. In our example above, the key = "Content-Type" and the value = "application / x-www-form-urlencoded".
As an alternative to the definition of header options, the values can first be collected in a dictionary. Here too, the key and the value as described above must be defined.

The lower code snippet describes the process in C #.
m_Request.AddHeader("Content-Type", "application/x-www-form-urlencoded");

//Alternative
Dictionary<string, string> m_Headers = new Dictionary<string, string>();
m_Headers.Add("Content-Type", "application/x-www-form-urlencoded");
foreach (KeyValuePair<string, string> m_Header in m_Headers)
{
    m_Request.AddHeader(m_Header.Key, m_Header.Value);
} 

Once the request has been compiled, the client can be instantiated. This client starts the request and returns the result (response). There are some Execute methods available that need to be selected according to the situation. An asynchronous execution is preferable so that the application does not stop.

Only if the StatusCode = OK is an evaluatable result available in the Content response property. In the case of Forge, the format is a JSon format and should be evaluated accordingly.

RestClient m_RestClient = new RestClient("https://developer.api.autodesk.com");
IRestResponse m_Response = await m_RestClient.ExecuteTaskAsync(m_Request);

switch(m_Response.StatusCode)
{
    case System.Net.HttpStatusCode.OK:

        string m_Result = m_Response.Content;

        break;
} 

Copyright

© Copyright 2019 by CAD-Becker.de

Visual Lisp To C#: OOPS - Was ist die objektorient...
AutoCAD: Tipps zur Bearbeitung von großen Zeichnun...

Related Posts

 

About me

  Dipl.-Ing.
Jürgen A. Becker
Versorgungstechnik

 

 

About us

Contact

Jürgen A. Becker

CAD-Becker.de
Detmolder Str. 786
33699 Bielefeld
Germany

Telephone
+49 (5202) 9953808 
Mobile
+49 170 870 8679
E-Mail
Juergen.Becker(at)CAD-Becker.de