6
$\begingroup$

I want to automatically get short links equivalents for things I CloudDeploy. I'd appreciate some tips on integrating command line workflow below into Mathematica function. For command line, using free API key, I do

To shorten www.google.com on command line, I execute following:

curl https://www.googleapis.com/urlshortener/v1/url?key=$key \
  -H 'Content-Type: application/json' \
  -d '{"longUrl": "http://www.google.com/"}'

This will print the following to stdout, thing next to id contains the shortened URL.

{
 "kind": "urlshortener#url",
 "id": "https://goao.gl/fbsS",
 "longUrl": "http://www.google.com/"
}
$\endgroup$
  • $\begingroup$ I was not aware of URLShorten, that looks like a better solution $\endgroup$ – Yaroslav Bulatov Apr 24 '17 at 22:23
7
$\begingroup$

Many URL* functions overlap with functionality so there there are many ways.

Here is an example which returns a list of rules:

URLExecute @ HTTPRequest[
    "https://www.googleapis.com/urlshortener/v1/url"
  , <| "Method" -> "POST"
     , "Headers" -> {"Content-Type" -> "application/json"}
     , "Query" -> {"key" -> key}
     , "Body" -> ExportString[{"longUrl" -> "http://www.google.com/"}, "JSON"]
    |>
  , CharacterEncoding -> None (*becasue export to json already did it*)
]
{"kind" -> "urlshortener#url", "id" -> ..., 
 "longUrl" -> "http://www.google.com/"} 

But, as mentioned earlier, you can use WRI's shortener with URLShorten.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.