Search code examples
c#asp.net-mvc.net-corebitly

Bitly shorten URL - getting status as WaitingForActivation


I am trying to generate short link Using bitly but getting "Status = WaitingForActivation"

public async Task<string> GetShortUrl(string url)
{
    var myAccessToken = "Token";
    var client = new HttpClient();
    client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer {myAccessToken}");

    var content = new
    {
        long_url = url
    };
    //HTTP POST
    var resp = await client.PostAsJsonAsync("https://api-ssl.bitly.com/v4/shorten", content);
    var link = await resp.Content.ReadAsStringAsync();
    return link;
}

[HttpGet("redirectUser/{PlanId}")]
        [ValidateModelState]
        public async Task<IActionResult> sendNotification(Guid PlanId)
        {
             var surveylink = "https://chats.landbot.io";
             // getting Status = WaitingForActivation
             var shortLink = GetShortUrl(surveylink); 
            }
        }

I am getting Status = WaitingForActivation at

var shortLink = GetShortUrl(surveylink); 


Solution

  • The simple reason for this issue is that when the below given love is being executed :-

    var resp = await client.PostAsJsonAsync("https://api-ssl.bitly.com/v4/shorten", content);
    

    It is causing the code to move to the next line without completing the current part. So, the next line :-

    var link = await resp.Content.ReadAsStringAsync();
    

    is unable to execute properly as a result.

    What you can do to solve this issue is create a waiting period till some response is returned. Once a response is returned, then move to the next line and then return the link.