Invoke Curl from within shell script - linux

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.

Related

Passing a URL with brackets to curl using bash script

I am trying to get the response from a curl url only for a particular value. For example
i am using the command
URLS=$(curl -g -H "Authorization: ${abc}" "https://api.buildkite.com/v2/organizations/org/agents?meta_data=[queue=dev]")
echo "${URLS}"
The metadata is actually as below:
"meta_data": [
"queue=dev"
]
The above curl command is giving the response for all agents in all queues and not able to get the required ones specific to queue=dev.
What is the correct way to pass url with brackets?

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

zsh: no matches found when $ curl -s -X localhost:60702/api/bundle?name=light%20reading | j q '.'

I am working through the Node.js The Right Way book by Jim Wilson. I am currently trying to use a PUSH request to create a new bundle with the specified name.
* curl -X POST http://:/api/bundle?name=
However, when I use the command:
$ curl -s -X POST localhost:60702/api/bundle?name=light%20reading | jq '.'
rather than getting the JSON indicating that a Bundle has been created, I get: zsh: no matches found: localhost:60702/api/bundle?name=light%20reading
The command should be using a POST request to create a new All of my code is bit for bit identical to the code listed in the book. Any ideas?
Can you try
curl -s -X POST 'localhost:3000/api/bundle?name=light%20reading'
i.e wrap the url within '
This seems to be an issue with zsh solved here.
There are several ways to solve this:
You can escape the question mark ? in the url by quoting the url as explained by #huzaifa-saifuddin to avoid zsh treating it as a wildcard character.
As explained here, you can create an alias for curl: alias curl='noglob curl'
As explained here, you can disable to nomatch handling by adding the following to your ~/.zshrc: unsetopt nomatch

Print HTML Response with localhost cUrl - Linux

I want to execute PHP Script on my Ubuntu Virtual Machine but I only have access to the command line. I've thought about using cUrl but I have a problem with it :
When I use the following command : "curl http://localhost/myscript.php"
The response is the plain file ("<?php echo "<p>Hello world</p>"; ?>") instead of html response ("<p>Hello world</p>")
How to solve this problem ?
Thank you
You may have to set the content-type using header().
Before the echo statement, insert this:
header('Content-Type: text/html');
See: http://php.net/manual/en/function.header.php

How to get this applescript working?

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

Resources