fay
fay

Reputation: 15

localhost route not working should be returning JSON back when locating to URL

My fellow programmers, I basically have this async Get() method which is reading Json data successfully, but when locating to route -> localhost:59185/api/encompass/data I receive a message:

No HTTP resource was found that matches the request URL 'http://localhost:59185/api/encompass/data'.
</Message>

I was very hopeful that it would return my JSON especially when in debug the code its sitting in 'string res' at the bottom

anyone know why its not returning Json even thought its sitting in 'res'?

Controller:

    [HttpGet, Route("encompass/data")]
    public async Task<string> Get(string Accesstoken)
    {
         string res = "";
         using (var client = new HttpClient())
        {
            Accesstoken = Accesstoken.Substring(17, 28);
            client.BaseAddress = new Uri("https://api.elliemae.com/");
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Accesstoken);
            var response = client.GetAsync("encompass/v1/loans/ea7c29a6-ee08-4816-99d2-fbcc7d15731d").Result;
            using (HttpContent content = response.Content)
            {
                // ... Read the string.
                Task<string> result = content.ReadAsStringAsync();
                res = result.Result;
            }

            return res; //<- this is not returning the JSon thats sitting in here 
        }

    }

Upvotes: 0

Views: 308

Answers (1)

Mr Slim
Mr Slim

Reputation: 1458

fay, the bearer token used in the header of your Get method must be Base64 encoded

See [how-do-i-encode-and-decode-a-base64-string][1]

I have added a Bearer HEADER value for the token.

  [HttpGet, Route("values/get")]
        public async Task<string> Get(string resulted)
        {

            string res = "";
            using (var client = new HttpClient())
            {
                // HTTP POST

                client.BaseAddress = new Uri("https://api.elliemae.com/");          
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(resulted)));
                var response = client.GetAsync("/encompass/v1/loans/{ea7c29a6-ee08-4816-99d2-fbcc7d15731d}?Authorization=Bearer "+resulted+"&Content-Type=application/json").Result;

                using (HttpContent content = response.Content)
                {
                    // ... Read the string.
                    Task<string> result = content.ReadAsStringAsync();
                    res = result.Result;
                }
            }
            return res;
        }

Upvotes: 0

Related Questions