eugneio dsjgfo
eugneio dsjgfo

Reputation: 131

Shorting links with bitly is truncating my full URL

I'm using vb.net to my web and I'm trying to short my URL with Bitly. So far so good. Now, when I use multiple parameters in a URL, it being truncated.

URL example: https://www.google.com/search?ei=JZJuXICFOLXaxgPqyrXYDQ&q=github&oq=github&gs_l=psy-ab.3..0i131i67j0i67j0l8.5770.6610..6785...0.0..1.193.560.5j1....3..0....1..gws-wiz.......0i71j0i131.TSeJXtGZpvc

URL result: https://www.google.com/search?ei=JZJuXICFOLXaxgPqyrXYDQ

As you can see, the URL always is truncated when it finds the first "&".

Do know what is happening?

This is an example of my code:

Option Infer On

Imports System.Net
Imports System.Web

Module Module1
    Sub Main()
        Dim wc = New WebClient()
        Dim login = "mylogin"
        Dim apiKey = "R_aaaaaaaaaaaaa1a11a11a11a11a1a11a1"
        Dim longUrl = System.Uri.EscapeUriString("https://www.google.com/search?ei=JZJuXICFOLXaxgPqyrXYDQ&q=github&oq=github&gs_l=psy-ab.3..0i131i67j0i67j0l8.5770.6610..6785...0.0..1.193.560.5j1....3..0....1..gws-wiz.......0i71j0i131.TSeJXtGZpvc")
        Dim request = String.Format("http://api.bit.ly/v3/shorten?login={0}&apiKey={1}&longUrl={2}&format=txt", login, apiKey, longUrl)
        Dim result = wc.DownloadString(request)
    End Sub

End Module

Upvotes: 1

Views: 628

Answers (1)

AakashM
AakashM

Reputation: 63348

Consider what your request string actually looks like. I have inserted line breaks for readability, of course there aren't any in the real thing:

http://api.bit.ly/v3/shorten?login=mylogin
    &apiKey=R_aaaaaaaaaaaaa1a11a11a11a11a1a11a1
    &longUrl=https://www.google.com/search?ei=JZJuXICFOLXaxgPqyrXYDQ
    &q=github
    &oq=github
    &gs_l=psy-ab.3..0i131i67j0i67j0l8
        .5770.6610..6785...0.0..1.193.560.5j1....3..0....1..
        gws-wiz.......0i71j0i131.TSeJXtGZpvc
    &format=txt

Do you see the problem? How is bitly supposed to know that &q=github and so on are part of longUrl, and not extra parameters?

The fix is simple. As the documentation says under Best Practices | Encoding:

URL Encoding

All long URLs sent to the Bitly API must be URL encoded, even if these links already contain escaped characters. For more information about URL encoding, see this Wikipedia article.

Upvotes: 1

Related Questions