Guest User

Untitled

a guest
Mar 25th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Newtonsoft.Json;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Net;
  9.  
  10. namespace API.Controllers
  11. {
  12.     [ApiVersion("1.0")]
  13.     [Route("api/v{version:apiVersion}/[controller]")]
  14.     [ApiController]
  15.     public class ExternalApiController : Controller
  16.     {
  17.         private string ExternalApiLink = "https://blablabla.com/api";
  18.         private string ExternalApiLinkGet = "/module/1/";
  19.         [HttpGet("getdata")]
  20.         public ActionResult<ExternalApi> GetDataFromExternal()
  21.         {
  22.  
  23.             using (var client = new HttpClient())
  24.             {
  25.                 client.BaseAddress = new Uri(ExternalApiLink);
  26.                
  27.                 var requestApi = client.GetAsync(ExternalApiLinkGet);
  28.                 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "XXXX");
  29.                 requestApi.Wait();
  30.  
  31.                 var resultFromApi = requestApi.Result;
  32.  
  33.                 if (resultFromApi.IsSuccessStatusCode)
  34.                 {
  35.                     var readResponse = resultFromApi.Content.ReadAsAsync<IList<ExternalApi>>();
  36.                     readResponse.Wait();
  37.  
  38.                     var data = readResponse.Result;
  39.                    
  40.                     return Json(data);
  41.                 }else
  42.                 {
  43.                     return NotFound();
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment