-1

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.

Serge
  • 40,935
  • 4
  • 18
  • 45
thomsan
  • 433
  • 5
  • 19
  • The answer in [that question](https://stackoverflow.com/q/58956527/5803406) is probably the same answer to this question. Either pull in the nuget package or create your own extension method to deserialize with System.Text.Json – devNull Apr 24 '21 at 17:07
  • It's not directly related to your question, and you may already be aware - but you can have your `Main` return `async Task`, which will allow you to await the async calls in your sample - https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#async-main – Andrew Hillier Apr 24 '21 at 17:22
  • Please refer this https://stackoverflow.com/questions/14520762/system-net-http-httpcontent-does-not-contain-a-definition-for-readasasync-an – Bibin Gangadharan Apr 24 '21 at 17:23
  • @devNull Can u please help me, how can I apply that answers for my problem? – thomsan Apr 24 '21 at 17:34

2 Answers2

3

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);
Katana
  • 752
  • 4
  • 23
0

if you need just a shortLink

var json = response.Content.ReadAsStringAsync().Result;
var result=JObject.Parse(json);

var shortLink = result["url"]["shortLink"].ToString();
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Yeah I tried it, dataObjects value showing as `{"url":{"status":7,"fullLink":"https:\/\/www.google.com","date":"2021-04-24","shortLink":"https:\/\/cutt.ly\/Test","title":"Google"}}` How can I get only `shortLink` – thomsan Apr 24 '21 at 17:32
  • I updated my answer. It will be result.Url.ShortLink; – Serge Apr 24 '21 at 17:45