youtube api v3 search through bash and curl - linux

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.

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?

how to pass float value as query params using curl?

I have a python flask server running and the following http GET works on my browser, http://example.com/v1/api?lng=18.565810740668912&lat=-33.93153605161741 , however when I use command curl http://example.com/v1/api?lng=18.565810740668912&lat=-33.93153605161741 it doesn't work. How to correct my query to work for curl?
You have to pass URL as a string.
It's because for shell a lot of characters used in URL have special meaning (ie = is an assignment, & makes command to run in background, etc). So
curl "http://example.com/v1/api?lng=18.565810740668912&lat=-33.93153605161741"
should work as expected.
To pass the query-parameters separated and let cURL build the query-string we can also use option -G together with usual -d like
curl -G -d 'lng=18.565810740668912' -d 'lat=-33.93153605161741' http://example.com/v1/api
See also: Construct a Query String (TLDR: Use -G argument)

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

dates in couchdb

How do you insert dates in couchdb? As strings?
I have couchdb-1.0.3.
1.
I did this:
$ curl -X PUT 127.0.0.1:5984/misc/doc1 -d '{"date":"2011-13-01T17:30:12+01:00"}'
This works, but this date doesn't exists.
2.
I thought I had to do this:
$ curl -X PUT 127.0.0.1:5984/misc/doc1 -d '{"date":new Date("2011-12-01)}'
But this is invalid JSON.
3.
When I use this format,
$ curl -X PUT 127.0.0.1:5984/misc/doc1 -d '{"date":"2011/12/01 00:00:00"}'
I doesn't work well with this format
$ curl -X GET '127.0.0.1:5984/misc/_design/foo/_view/view1?startkey="2012-02-02"'
Because the document shows up in the result.
Thanks,
Eric J.
I suggest that you use your first format, or possibly the JSON2 standard format, which is most convenient for JavaScript. That is what most people do, and it works well with your example request:
$ curl '127.0.0.1:5984/misc/_design/foo/_view/view1?startkey="2012-02-02"'
To validate your data, use a validation function.

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