Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a small "learning" program to shorten links from the "https://tiny.cc" service.

But I'm new to using the "API" so I'm having a hard time understanding some points
I have searched a lot and tried to read the explanation of the site

https://tinycc.com/tiny/api-docs#shorten



And trying to implement it, but without the correct result
So please help

this is My Code

What I have tried:

C#
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
   
public static async Task<string> tinyccShortenerAsync(string tinyccAPIKey, string LongURL)
        {
            string ShortURL;

            var payload = new
            {
                long_url = LongURL,
            };

            using (var httpClient = new HttpClient { BaseAddress = new Uri("https://tiny.cc/tiny/api/3/") })
            {
                httpClient.DefaultRequestHeaders.Add("apikey", tinyccAPIKey);

                var body = new StringContent(JsonConvert.SerializeObject(payload), UnicodeEncoding.UTF8, "application/json");

                using (var response = await httpClient.PostAsync("urls/", body))
                {
                    response.EnsureSuccessStatusCode();

                    var link = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());

                    ShortURL = "https://" + link.short_url;
                }
            }

            return ShortURL;
        }


static async Task Main(string[] args)
{

    var task = await tinyccShortenerAsync("TEST_API","http://www.google.com/");

    Console.WriteLine(task);
    Console.ReadLine();

}
Posted
Comments
Richard MacCutchan 15-Apr-22 11:01am    
You have not explained what the problem is.
Eng Mohamed Bassuny 16-Apr-22 11:27am    
i cant make POST method ,
this code doesn't get me anything,

how i can make code to send long url and recive Short url in "https://tiny.cc" service.

this is my problem

Richard MacCutchan 16-Apr-22 12:05pm    
Sorry, I have no idea. You need to read the instructions on the tiny.cc website.
Aravindba 19-May-22 5:48am    
what actually return in ShortURL variable ? or any error ?
Aravindba 19-May-22 5:48am    
what actually return in ShortURL variable ? or any error ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900