gEEEk Posted January 18, 2011 Share Posted January 18, 2011 Hello dudes. I'm just messing around with the Google URL Shortner API just for knowledge purposes, but I am having some issues with variables in curl. Its super simple, but its just for fun. BTW, the script wont run if you try because there is no API key in that syntax. Allright, I think the problem is with the variable handling in the curl syntax. But I'm not sure, I cant really see why it just wont use the user input from the previous lines... echo "Gimme the URL boy!" read url curl https://www.googleapis.com/urlshortener/v1/url?key=XXXXXXXXXX \ -H 'Content-Type: application/json' \ -d '{"longUrl": "$url"}' Its giving me this error: ( "{", " \"error\": {", " \"errors\": [", " {", " \"domain\": \"global\",", " \"reason\": \"invalid\",", " \"message\": \"Invalid Value\",", " \"locationType\": \"parameter\",", " \"location\": \"resource.longUrl\"", " }", " ],", " \"code\": 400,", " \"message\": \"Invalid Value\"", " }", "}" Quote Link to comment Share on other sites More sharing options...
Netshroud Posted January 19, 2011 Share Posted January 19, 2011 Are you sure it's substituting $url correctly? Quote Link to comment Share on other sites More sharing options...
gEEEk Posted January 19, 2011 Author Share Posted January 19, 2011 I think that is the problem, however If I'm just issuing a simple: " echo $url " I'm getting the url from the user input. I think that curl has some issues handling variables from a bash script. Not sure though.. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 19, 2011 Share Posted January 19, 2011 echo "Gimme the URL boy!" read url curl https://www.googleapis.com/urlshortener/v1/url?key=XXXXXXXXXX \ -H 'Content-Type: application/json' \ -d '{"longUrl": "$url"}' I think the problem is that you have the $url within single quotes. Single quotes will not do variable replacements, it will be passing the longUrl as $url rather than the value contained in $url. Possible Solution, swap your double and single quotes round on the -d line. Quote Link to comment Share on other sites More sharing options...
Netshroud Posted January 19, 2011 Share Posted January 19, 2011 That's what I was thinking too. If the API works over HTTP, double-check with Wireshark. Quote Link to comment Share on other sites More sharing options...
gEEEk Posted January 19, 2011 Author Share Posted January 19, 2011 Hmm.. { "error": { "errors": [ { "domain": "global", "reason": "internalError", "message": "Internal Error" } ], "code": 500, "message": "Internal Error" } } The API dosent seem to like that I'm replacing the quotes. . Whata jesus, those this mean that the API cant handle variables.. Atleast not if you're using it though curl? Quote Link to comment Share on other sites More sharing options...
Netshroud Posted January 19, 2011 Share Posted January 19, 2011 No idea what language that is, but can't you do something like: echo "Gimme the URL boy!" read url json = '{"longURL": "' + $url + '"}' curl https://www.googleapis.com/urlshortener/v1/url?key=XXXXXXXXXX \ -H 'Content-Type: application/json' \ -d $json ? Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 20, 2011 Share Posted January 20, 2011 The API dosent seem to like that I'm replacing the quotes. . Whata jesus, those this mean that the API cant handle variables.. Atleast not if you're using it though curl? It isn't the API, its the shell. Or rather your use of the quotes in the shell. Try something like #!/bin/sh echo "Gimme the URL boy!" read url curl https://www.googleapis.com/urlshortener/v1/url \ -H 'Content-Type: application/json' \ -d "{'longUrl': '$url'}" and it should work fine, or it did when I tested it. The problem you had was that you were passing curl a JSON string to pass to the google's API. When you enclosed the JSON string in single quotes you were telling the shell to not expand any variables in it so '{"longUrl": "$url"}' will pass to the API {"longUrl": "$url"} When you enclosed the JSON string in double quotes you are telling the shell you want it to expand any variables it finds, so "{'longUrl': '$url'}" will pass to the api {'longUrl': 'Whatever URL you put in the variable'} Try playing with single and double quotes with variables in the shell using the echo command. After playing around a bit it should all start to make sense. Quote Link to comment Share on other sites More sharing options...
gEEEk Posted January 20, 2011 Author Share Posted January 20, 2011 Brilliant mate, solved the entire thing! I'm so happy I could probably kiss ya, no homo ofc. :) Now, to finalize the script. I want it to GREP out the new URL. How would I go about that? The output is: { "kind": "urlshortener#url", "id": "http://goo.gl/XxXX", "longUrl": "http://google.com/" } grep "id" dosent seem to work.. Do I need to set the specific character lenght to grep out? Or idk.. : / Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 20, 2011 Share Posted January 20, 2011 Now, to finalize the script. I want it to GREP out the new URL. How would I go about that? I wouldn't use grep to do that I would use sed. #!/bin/sh echo "Gimme the URL boy!" read url curl https://www.googleapis.com/urlshortener/v1/url \ -H 'Content-Type: application/json' \ -d "{'longUrl': '$url'}" 2>/dev/null | sed -r -e '/"id"/ !d' -e '/"id"/ s|.*"id".*(http://.*)",|\1|' First the "2>/dev/null" part throws away some of the output that curl is putting out via stderr on my machine. Now the sed part has 3 parameters. The first ( -r ) sets sed to use more advanced regular expressions (On some OS's it is -E). The second parameter ( -e '/"id"/ !d' ) deletes any lines that don't have "id" in them. The third parameter ( -e '/"id"/ s|.*"id".*(http://.*)",|\1|' ) takes any line with "id" in it and pulls out the url. It then replaces the pattern with the url it has found. This can be a little confusing to begin with so read up on sed to get to grips with it. The second parameter could be replaced by piping the stream though grep first and grepping for "id". There are also many other ways that you could achieve the same sort of results (e.g. using awk). Quote Link to comment Share on other sites More sharing options...
gEEEk Posted January 20, 2011 Author Share Posted January 20, 2011 Amazing dude, thanks for all your help. I ended up changing the -r parameter to -> -E since I am on OSX. / gEEEk Quote Link to comment Share on other sites More sharing options...
HacDan Posted January 22, 2011 Share Posted January 22, 2011 (edited) http://code.google.com/apis/urlshortener/v1/getting_started.html Just contributing for those looking for more information. Google typically documents their API's pretty clearly. =) Edited January 22, 2011 by Hac-Dan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.