simple code to call rest api c#

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers; 

namespace ConsoleProgram
{
    public class DataObject
    {
        public string Name { get; set; }
    }

    public class Class1
    {
        private const string URL = "https://sub.domain.com/objects.json";
        private string urlParameters = "?api_key=123";

        static void Main(string[] args)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            // List data response.
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call! Program will wait here until a response is received or a timeout occurs.
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body.
                var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;  //Make sure to add a reference to System.Net.Http.Formatting.dll
                foreach (var d in dataObjects)
                {
                    Console.WriteLine("{0}", d.Name);
                }
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }

            //Make any other calls using HttpClient here.

            //Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();
        }
    }
}

4
8
Tvsjhuser 110 points

                                    namespace RestSharpThingy
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Reflection;

    using RestSharp;

    public static class Program
    {
        public static void Main()
        {
            Uri baseUrl = new Uri(&quot;https://httpbin.org/&quot;);
            IRestClient client = new RestClient(baseUrl);
            IRestRequest request = new RestRequest(&quot;get&quot;, Method.GET) { Credentials = new NetworkCredential(&quot;testUser&quot;, &quot;P455w0rd&quot;) };

            request.AddHeader(&quot;Authorization&quot;, &quot;Bearer qaPmk9Vw8o7r7UOiX-3b-8Z_6r3w0Iu2pecwJ3x7CngjPp2fN3c61Q_5VU3y0rc-vPpkTKuaOI2eRs3bMyA5ucKKzY1thMFoM0wjnReEYeMGyq3JfZ-OIko1if3NmIj79ZSpNotLL2734ts2jGBjw8-uUgKet7jQAaq-qf5aIDwzUo0bnGosEj_UkFxiJKXPPlF2L4iNJSlBqRYrhw08RK1SzB4tf18Airb80WVy1Kewx2NGq5zCC-SCzvJW-mlOtjIDBAQ5intqaRkwRaSyjJ_MagxJF_CLc4BNUYC3hC2ejQDoTE6HYMWMcg0mbyWghMFpOw3gqyfAGjr6LPJcIly__aJ5__iyt-BTkOnMpDAZLTjzx4qDHMPWeND-TlzKWXjVb5yMv5Q6Jg6UmETWbuxyTdvGTJFzanUg1HWzPr7gSs6GLEv9VDTMiC8a5sNcGyLcHBIJo8mErrZrIssHvbT8ZUPWtyJaujKvdgazqsrad9CO3iRsZWQJ3lpvdQwucCsyjoRVoj_mXYhz3JK3wfOjLff16Gy1NLbj4gmOhBBRb8rJnUXnP7rBHs00FAk59BIpKLIPIyMgYBApDCut8V55AgXtGs4MgFFiJKbuaKxq8cdMYEVBTzDJ-S1IR5d6eiTGusD5aFlUkAs9NV_nFw&quot;);
            request.AddParameter(&quot;clientId&quot;, 123);

            IRestResponse&lt;RootObject&gt; response = client.Execute&lt;RootObject&gt;(request);

            if (response.IsSuccessful)
            {
                response.Data.Write();
            }
            else
            {
                Console.WriteLine(response.ErrorMessage);
            }

            Console.WriteLine();

            string path = Assembly.GetExecutingAssembly().Location;
            string name = Path.GetFileName(path);

            request = new RestRequest(&quot;post&quot;, Method.POST);
            request.AddFile(name, File.ReadAllBytes(path), name, &quot;application/octet-stream&quot;);
            response = client.Execute&lt;RootObject&gt;(request);
            if (response.IsSuccessful)
            {
                response.Data.Write();
            }
            else
            {
                Console.WriteLine(response.ErrorMessage);
            }

            Console.ReadLine();
        }

        private static void Write(this RootObject rootObject)
        {
            Console.WriteLine(&quot;clientId: &quot; + rootObject.args.clientId);
            Console.WriteLine(&quot;Accept: &quot; + rootObject.headers.Accept);
            Console.WriteLine(&quot;AcceptEncoding: &quot; + rootObject.headers.AcceptEncoding);
            Console.WriteLine(&quot;AcceptLanguage: &quot; + rootObject.headers.AcceptLanguage);
            Console.WriteLine(&quot;Authorization: &quot; + rootObject.headers.Authorization);
            Console.WriteLine(&quot;Connection: &quot; + rootObject.headers.Connection);
            Console.WriteLine(&quot;Dnt: &quot; + rootObject.headers.Dnt);
            Console.WriteLine(&quot;Host: &quot; + rootObject.headers.Host);
            Console.WriteLine(&quot;Origin: &quot; + rootObject.headers.Origin);
            Console.WriteLine(&quot;Referer: &quot; + rootObject.headers.Referer);
            Console.WriteLine(&quot;UserAgent: &quot; + rootObject.headers.UserAgent);
            Console.WriteLine(&quot;origin: &quot; + rootObject.origin);
            Console.WriteLine(&quot;url: &quot; + rootObject.url);
            Console.WriteLine(&quot;data: &quot; + rootObject.data);
            Console.WriteLine(&quot;files: &quot;);
            foreach (KeyValuePair&lt;string, string&gt; kvp in rootObject.files ?? Enumerable.Empty&lt;KeyValuePair&lt;string, string&gt;&gt;())
            {
                Console.WriteLine(&quot;\t&quot; + kvp.Key + &quot;: &quot; + kvp.Value);
            }
        }
    }

    public class Args
    {
        public string clientId { get; set; }
    }

    public class Headers
    {
        public string Accept { get; set; }

        public string AcceptEncoding { get; set; }

        public string AcceptLanguage { get; set; }

        public string Authorization { get; set; }

        public string Connection { get; set; }

        public string Dnt { get; set; }

        public string Host { get; set; }

        public string Origin { get; set; }

        public string Referer { get; set; }

        public string UserAgent { get; set; }
    }

    public class RootObject
    {
        public Args args { get; set; }

        public Headers headers { get; set; }

        public string origin { get; set; }

        public string url { get; set; }

        public string data { get; set; }

        public Dictionary&lt;string, string&gt; files { get; set; }
    }
}

4 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
http api call in c# api rest c# .net request response through api with c# make rest call in C# c# api calling api how to make a rest get request in C# c# working with rest api rest API in C#.net rest API in C3.net web api rest service c# example how to request on api in c# run rest api in c# code &quot;Rest Api in Asp.Net and C#&quot; restful api request example c# rest api create c# how to make api call with c# rest api c# net methode get api rest exemple c# rest api .net c# rest api services in c# Web API and REST api C# create c# rest api json c# rest api with asp.net call api from c# project call web api from c# code behind call api once web api c# how to call a web api in c# how to send rest api request in c# how to use rest api in asp.net c# c# calling rest service example create rest api with c# c# build rest api http server how to make api call in c# call a get api from c# create api rest c# client api request c# call api from api c# can rest api can create using c# how to consume a rest api in c# rest api implementation in c# build restful api c# how to make simple rest api c# restapi request with c# how to work with api in c# c# api call web request build rest api with c# how to make api calls c# c# api calls rest api in c# sample code rest api in c# tutorial sample code to call API through C# c# best rest api request easily call APIs from c# Rest api call example c# rest api using c# c# simple rest api server c# simple http api create rest api c#.net call rest api using c# C# REST API tutorial restapi c# get rest api C# tutorial how to call api in api controller c# c# rest api get create rest api c# tutorial build rest api c# how to call api in c# C# how to call a api call api add request rest c# http rest get c# call rest api from c# console application c# request api consume rest api c# how to consume api using rest sharp c# simple rest api C# c# calling rest api how to rest api in c# how to create rest api in c# step by step c# how to make call api c# how to call an api from inside api c# api rest c# perform rest request c# create api call how to call rest api from c# code how to call restapi from c # code c# best way to call rest api make api call c# Rest api response c# c# make api request how to consume rest api in c# web application call api from c# c# call api basic api rest c# example basic api testing code c# rest api get rest api in c# c# http request for api call asp.net call rest api c# step by step how to make an api call in c# send api request c# call simple api in c# get restful API in C# c# how to call web api c# create rest client c# restful api step by step c# calling a rest api asp.net make rest api call how to call web api from web api c# make call api c# how to develop rest api in c# call get rest api from c# create rest request c# how to call a simple rest api c# make request to api c# build a rest api c# create a rest api in c# c# rest request example call an api from c# web api c# example how to make an api call C# c# rest apis how to call api in c#? how to create rest api in asp.net c# rest call c# how to create a rest api in c# How to create a rest api C# how to make a get request in web api c# call http api using c# how to make an api c# c# get rest api C# make a call to an api api sample code c# create restful api c# c# using rest api c# call simple rest api Get response from web API in c# how to make api call from c# REST APIs with C# create rest api c# call web api in c# call api in c# web api sample code c# rest api create c# client simple api get request c# C# calling a api from an api call api from code behind c# C# web api request send object to rest api c# restful web api c# example how to build rest request c# rest api web api c# calling api from c# calling rest api from c# rest client c# how to use c# make rest call how to send request api rest c# c# request from api create rest api in asp.net c# rest api tutorial in c# create response web api c# how to build a rest api in c# rest api calling in c# fastest way to make api call in c# How to call a REST api in c# call web api from c# c# send api request c# call api get c# making api request simple code to call rest api c# api call c# rest web api c# call rest web service from c# using rest client calling rest api using rest client c# creating rest api program using c#.net creating rest api program using c# rest api call c# rest request c# build a rest api in c# creating c# rest api .net make rest api call api request and response c# make a request to api dotnet simple rest api calls in c# set up rest api c# make a rest call c# c# call to rest api rest api c# create web api rest c# create an rest api c# how to make rest api in asp.net c# .net how to create call to Rest API rest c# request c# call rest api .net use rest api in c# sample c# code to call rest api API REST C# rest api example c# http rest c# get request from api c# rest api c# httprequest c# call restful api consume rest api in c# creating rest api in c# C# calling rest api example rest api c# example how to call rest api from asp.net c# api rest c# c# create rest api simple rest request c# c# simple rest api request c# rest api request rest api with c# Make HTTP rest request in c# how to create rest api in c# rest api get example C# rest api c# create rest api in C# c# consume rest api tutorial c# rest api example rest api in c# connect rest api c# how to use rest api in c# what is rest api in c# C# read from api C# .net communicate with rest api calling rest api from c# .net 4.0 rest request types c# c# api request rest api example in C# access rest api c# how to call a restfull api in c# how do you make an API request in C# c# make a rest request call rest api in c# cal a restful api in a web service c c# rest request c# make call to rest api c# read string from rest api how to call api C# rest api using c# .net example c# example rest api C# different rest calls c# api call example C# resr withput module c# own rest client call external https api service in c# rest api call from .net c# rest api C# client asking API REST como criar chamar api asp.net best rest client c# REST classes in c# for http c# get api json restful client c# to read data calling simple api from c# api call in c# c# consume rest api c# call apis rest request in c# restful api in C# get c# rest call json example asp.net call rest api make api calls in c# with inbuilt method C# code to make rest api call using json how to consume rest api in c# rest api call in c# class c# client library rest all c# how to call rest api send a rest request c# rest api .net web response c# wxample how to call rest api from c# how to call a rest api from c# make an api request in c# get request from my api to another api using webclient API GET request from my api to another api using webclient working with rest apis in c# call api rest c# write into rest api c# call rest webservice c# making rest api calls in c# C# make rest api call c sharp connect to rest api c# rest connect c# get data from rest api call rest service from asp.net c# c# example best way to call restful api call rest api c# c# get reast c# api call making an api call in c# c# make rest api call post api request c# c# calling api make rest call from c# api request in c# restful api request c# how to connect api with C# c# get api request best way to consume a http api from c# rest api c# call api fetch REST api c# c# code to call rest api calling an api in c# call rest api json c# call restful api c# calling a rest api from c# c# rest api call call an api in c# How do you make an API call in C# csharp rest call c# rest apl calling how to call an api in c# simple api call c# rest api call format in C# calling rest api in C3 call API da Web de um cliente .NET (C#) como aplicacao windows form cosome api com c# c# consume restfuls ervice json call api c# visual studio como fazer chamada endpoint c# calling an api to retrieve information c# call a rest api from c# c# rest api get json example c# call rest api C# send an API link c# call external rest api c# make api call get request rest api c# how to connect to json api in c# c# send request to rest api call rest endpoint in c# get rest api c# call api c# C# call api c# api calling call rest api from c# how to call a public api in c# call rest api parameters with axios rest api in flask how to create rest api server php
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source