1

I am learning to make a web api with .NET core and I followed this documentation.

https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1

I have made a new fresh project to start with and this is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace FortniteAPI.Controllers
{
    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        // GET api/values
        [HttpGet]
        public string Get()
        {
            return "https://api.fortnitetracker.com/v1/profile/{platform}/{nickname}";
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }
    }
}

I am using data from this website for my web API https://www.fortnitetracker.com/

The API link requires two parameters and that is platform and nickname. I used my own information but when I start the app I don't see the data.

I also have a header key to use it and this is shown in the documentation.

To use the API key you need to pass it along as a header with your requests.

I don't really understand this sentence.

Also I am programming in Ionic 3 and I used this api link with the HTTP get but it didn't work because of CORS. That's why I use a .NET core application to talk with the client.

Can someone point me in the right direction?

UPDATE

[HttpGet]
        public string Get()
        {
            HttpClient http = new HttpClient();
            http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("APIKEY", header);
            var data = http.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            return data;
        }

2 Answers 2

4

Fix it thanks for the support!

 [HttpGet]
        public string Get()
        {
            HttpClient http = new HttpClient();
            http.DefaultRequestHeaders.Add(schemename, header);
            var data = http.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            return data;
        }    
Sign up to request clarification or add additional context in comments.

Comments

3

Please Put this Code in your Method And Let me know if you need anything more.

HttpClient http = new HttpClient();
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");
var data =  http.PostAsync("Your Url", new StringContent("Data", Encoding.UTF32, "text/xml")).Result.Content.ReadAsStringAsync().Result;

//for calling get

var data2=http.GetAsync("").Result.Content.ReadAsStringAsync().Result;

4 Comments

Please Pass Values in headers using httpClient
Hi Lavit, thanks for you time! Look at my update and I get this error in my browser {"message":"No API key found in request"}
Yes I can accept the answer tomorrow thanks for your time!
No problem once you get time Accept the Answer @Fearcoder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.