7
$\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$
1
  • $\begingroup$ I was not aware of URLShorten, that looks like a better solution $\endgroup$ Apr 24, 2017 at 22:23

1 Answer 1

8
$\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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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