Search code examples
c#json.net-corehttpclient

How to use “ReadAsAsync” in .core


I'm need to use cutt.ly URL shorter API and I followed it documentation and this how I consumed it

Cutt.ly documentation

class Program
{
    private const string URL = "https://cutt.ly/api/api.php";
    private static string urlParameters = "?key=ddbbdd323230fbf3e0b9&short=https://www.google.com&name=Test";
    static void Main(string[] args)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);

        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync(urlParameters).Result;.
        if (response.IsSuccessStatusCode)
        {
            var dataObjects = response.Content.ReadAsAsync().Result;
            foreach (var d in dataObjects)
            {
                Console.WriteLine("{0}", d.ToString());
            }
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }

        client.Dispose();
        Console.ReadLine();
    }
}

But problem is I'm getting following compiling error.

'HttpContent' does not contain a definition for 'ReadAsAsync' and no accessible extension method 'ReadAsAsync' accepting a first argument of type 'HttpContent' could be found

I'm using .net core. How can I handle this using .net core. I found this question. but I'm not clear it's answers.


Solution

  • i did this before here You will have to install Newtonsoft.Json nuget package

    CuttlyApiKey is your api key and SelectedCustomText is custom name for your link, you can set string.empty if you dont want to set custom name

    public async Task<string> CuttlyShorten(string longUrl)
            {
    
                try
                {
                    string url = string.Format("https://cutt.ly/api/api.php?key={0}&short={1}&name={2}", CuttlyApiKey, HttpUtility.UrlEncode(longUrl), SelectedCustomText);
    
                    using (HttpClient client = new HttpClient())
                    using (HttpResponseMessage response = await client.GetAsync(url))
                    using (HttpContent content = response.Content)
                    {
                        dynamic root = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
    
                        string statusCode = root.url.status;
                        if (statusCode.Equals("7"))
                        {
                            string link = root.url.shortLink;
    
                            return link;
                        }
                        else
                        {
                            string error = root.status;
                            
                        }
    
                    }
                }
                catch (Exception ex)
                {
                    
                }
    
    
                return "error";
            }
    

    usage:

    var shortUrl = await CuttlyShorten(url);