How to get this applescript working? - node.js

So the goal of this little diversion is to be able to select a piece of text containing a URL and have OS X convert it to a short URL using the goo.gl URL shortener service.
To that end I have created a service using Automator that accepts text input and passes that input to a "Run AppleScript" action. The AppleScript is listed below.
I've installed node.js v0.10.33 and also installed a node package called json (http://trentm.com/json/)
The script below works fine as long as I don't pipe the output to the json node app.
The curl command with piping to the json node app works perfect in Terminal.
My guess is something with the shell environment is off, but I have no idea how to look into that.
Any help?
-- shorten URLs via goo.gl URL shortener
-- syntax derrived from Scott Lowe # blog.scottlowe.org
on run {input, parameters}
set curlCommand to "curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{\"longUrl\": \"" & input & "\"}' | /usr/local/bin/json id"
set jsonResult to (do shell script curlCommand)
return jsonResult
end run

A great tool for handling JSON in Applescript is the free App JSON Helper (App Store). It converts JSON to Applescript dictionaries. So my answer is very (very) similar to #regulus6633 post, but without text parsing the JSON:
set input to "http://stackoverflow.com/questions/26757656/how-to-get-this-applescript-working"
-- Note: without piping to grep at the end!
set curlCommand to "curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{\"longUrl\": \"" & input & "\"}'"
-- getting the full JSON answer
set jsonResult to do shell script curlCommand
-- using JSON Helper to translate JSON to Applescript dictionary
tell application "JSON Helper"
set shortURL to |id| of (read JSON from jsonResult)
end tell
return shortURL
Greetings, Michael / Hamburg

Why do you have "return curlCommand"? Don't you really want "return jsonResult"?
Admittedly I don't know anything about json but the result of the curl command is a string and I do know how to parse a string in applescript. Here's how I would write your code parsing it as a string. Note I used this webpage's url as "input"...
set input to "http://stackoverflow.com/questions/26757656/how-to-get-this-applescript-working"
set curlCommand to "curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{\"longUrl\": \"" & input & "\"}' | grep '\"id\"'"
set jsonResult to do shell script curlCommand
set shortURL to text 9 thru -2 of jsonResult
return shortURL

Related

Curl request with comma in the directory name in bash

I have problem to execute curl request in the directory has comma in the name using bash command line.
curl --request POST --form "file=#$PWD/input_file" http://HOSTURL.com > output_file
if the directory name is
"test" works
"test test" works
"test, test" doesn't work.
I tried many ways to escape characters like quotations, back slush, changing IFS... but still getting error "failed creating formpost data".
Could someone advise how I should treat such directory names?
This looks like a case curl isn't designed to handle. However, by passing the filename on stdin, you can avoid needing it to correctly parse that value at all.
curl --request POST --form "file=#-" http://HOSTURL.com <input_file >output_file

youtube api v3 search through bash and curl

I'm having a problem with the YouTube API. I am trying to make a bash application that will make watching YouTube videos easy on command line in Linux. I'm trying to take some video search results through cURL, but it returns an error: curl: (16) HTTP/2 stream 1 was not closed cleanly: error_code = 1
the cURL command that I use is:
curl "https://ww.googleapis.com/youtube/v3/search" -d part="snippet" -d q="kde" -d key="~~~~~~~~~~~~~~~~"
And of course I add my YouTube data API key where the ~~~~~~~~ are.
What am I doing wrong?
How can I make it work and return the search attributes?
I can see two things that are incorrect in your request:
First, you mistyped "www" and said "ww". That is not a valid URL
Then, curl's "-d" options are for POSTing only, not GETting ,at least not by default. You have two options:
Add the -G switch to url, which lets curl re-interpret -d options as query options:
curl -G https://www.googleapis.com/youtube/v3/search -d part="snippet" -d q="kde" -d key="xxxx"
Rework your url to a typical GET request:
curl "https://www.googleapis.com/youtube/v3/search?part=snippet&q=kde&key=XX"
As a tip, using bash to interpret the resulting json might not be the best way to go. You might want to look into using python, javascript, etc. to run your query and interpret the resulting json.

Invoke Curl from within shell script

I've below code in shell script
#!/bin/bash
oauth_consumer_key='sdfsfsd'
oauth_consumer_key_secret='1sdfsdfs1'
oauth_token='5wrwerwr476a1737fe09de2e4ew'
oauth_token_secret='ec2231779e4'
url='https url goes here'
token=$(./oauth $oauth_consumer_key $oauth_consumer_key_secret $oauth_token $oauth_token_secret GET $url)
curl_path='/usr/bin/curl'
curl_args="-H 'Authorization: $token'"
resp=$($curl_path $curl_args $url)
echo $resp
Here, I'm first running oauth.sh from this program by passing params & capturing output in token which works as expected. And this output looks as shown below:
OAuth oauth_consumer_key="45435", oauth_token="4r43", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1415328827", oauth_nonce="4535345", oauth_version="1.0", oauth_signature="bKeewO%2BTJ7IHjurhtaftn9dNfxA%3D"' 'my url goes here'
Now, I need to invoke curl command from this program by passing above as param as shown below:
curl -H 'Authorization: OAuth oauth_consumer_key="45435", oauth_token="4r43", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1415328827", oauth_nonce="4535345", oauth_version="1.0", oauth_signature="bKeewO%2BTJ7IHjurhtaftn9dNfxA%3D"' 'my url goes here'
When I run above curl command from terminal console, it works but from my shell script it gives error couldn't resolve OAuth
Can anyone help me out in fixing this issue?
Thanks!
It looks like this line is looking for "oauth" in the current directory.
token=$(./oauth
I would try putting the absolute path to oauth instead of ./oauth and see if that works.
If that doesn't work, I would strip it down a little and comment out everything after that line and just echo $token and see if it's showing the expected results and go from there.

How can I send a file's contents as a POST parameter using cURL?

I'm trying to use cURL to POST the contents of a file, as if I'd pasted that contents in to an html textarea. That's to say I don't want to upload the file, I just want a post parameter called foo to be filled with text from a file called bar.txt. bar.txt's contents may include newlines, quotes, and so on.
Is this possible?
Thanks.
Edit: I found out how to do it in the end:
curl --data-urlencode "foo#bar.txt" http://example.com/index.php
This will take the contents of the file bar.txt, url encode it, place the resultant string in a parameter called foo in a POST request of http://example.com/index.php.
I can't speak to whether the solutions others have suggested will work or not, but the one above seems like the best way.
You can by doing something like:
$ curl --data "foo:$(cat foo.txt)" http://localhost/yourfile.php
Note that you'll probably want to encode the file, as cacheguard said. To encode it in base64, just modify the previous command like this:
$ curl --data "foo:$(cat foo.txt | base64)" http://localhost/yourfile.php
You should encode/decode the content of your file (for instance by using the base64 command under Linux).
file foo.txt:
8<----------------------------
Hello World
I am a Secure Web Gateway
8<----------------------------
base64 foo.txt | base64 -d

How do I POST LF with curl command line tool?

I'm trying to POST to the HTTP gateway of an SMS provider (Sybase 365) using CURL from a Linux shell script.
I need to pass the following data (note the [ ] and LF characters)
[MSISDN]
List=+12345678
[MESSAGE]
Text=Hello
[END]
If I submit a file using the -F parameter, CURL removes the LF e.g.
curl -F #myfile "http://www.sybase.com/..."
results in this at the server (which is rejected)
[MSISDN]List=+12345678[MESSAGE]Text=Hello[END]
Is there anything I can do to avoid this or do I need an alternative tool?
I'm using a file containing my data for testing but I'd like to avoid that in practice and POST directly from the script.
Try using --data-binary instead of -d(ata-ascii).
From the manual:
--data-binary (HTTP) This posts data in a similar manner as --data-ascii does, although when using this option the entire context of the posted data is kept as-is.
If you want to post a binary file without the strip-newlines feature of the --data-ascii option, this is for you. If this option is used several times, the ones following the first will append data.
ETA: oops, I should read the question more closely. You're using -F, not -d. But --data-binary may be still be worth a shot.
Probably a silly thought, but I don't suppose it actually requires CRLF instead of just LF?
Alternatively, have you tried using the --data-binary option instead of -F?
I've got this working using -d
request=`printf "[MSISDN]\nList=$number\n[MESSAGE]\nText=$message\n[END]\n"`
response=`curl -s -u $username:$password -d "$request" http://www.sybase.com/...`
Curiously, if I use -d #myfile (where myfile contains LF separated text), it doesn't work.
I also tried --data-binary without success.
curl "url" --data-binary #myfile
posts new lines in the data [tested on curl 7.12.1]

Resources