please help me what am I doing wrong? Im doing telegram notifier via ruby in my Fastfile and gets that problem:
[!] Syntax error in your Fastfile on line 175: Fastfile:175: syntax error, unexpected tIDENTIFIER, expecting end
210...ication/json' --data '{"chat_id": "123456789", "media": [{"t...
211... ^~~~~~~
212Fastfile:175: syntax error, unexpected tINTEGER, expecting end
213... --data '{"chat_id": "123456789", "media": [{"type": "photo"...
214... ^~~~~~~~~
My code:
lane :detox do
images = Dir.glob("folder/*.png")
images.each do |image|
puts image
sh "curl --request POST --header 'Content-Type: application/json' --data '{"chat_id": "123456789", "media": [{"type": "photo", "media": "attach://image"}]' https://api.telegram.org/botToken/sendMediaGroup"
end
I see a few things wrong here. Firstly, you should escape all your quotation marks in the string following sh. Otherwise, the string is terminated early and you'll encounter errors.
Secondly, you opened 2 do blocks: lane :detox do and images.each do |image|, but you only have a single end.
This should be
lane :detox do
images = Dir.glob("folder/*.png")
images.each do |image|
puts image
sh "curl --request POST --header 'Content-Type: application/json' --data '{\"chat_id\": \"123456789\", \"media\": [{\"type\": \"photo\", \"media\": \"attach://image\"}]}' https://api.telegram.org/botToken/sendMediaGroup"
end
end
The syntax error is caused by using unescaped double quotes within a double quoted string:
str = "foo "bar" baz"
# ^^^^^ won't work
You can escape " as \" but it's often easier to use a heredoc instead which doesn't require escaping:
sh <<-CMD
curl https://api.telegram.org/botToken/sendMediaGroup \
--request POST \
--header 'Content-Type: application/json' \
--data '{"chat_id": "123456789", "media": [{"type": "photo", "media": "attach://image"}]}'
CMD
However, instead of building the command yourself, you can also pass the command options as separate arguments to sh:
sh(
'curl', 'https://api.telegram.org/botToken/sendMediaGroup',
'--request', 'POST',
'--header', 'Content-Type: application/json',
'--data', '{"chat_id": "123456789", "media": [{"type": "photo", "media": "attach://image"}]}'
)
Which also allows you to create the JSON from a Ruby hash on the fly:
require 'json'
data = {
chat_id: "123456789",
media: [{ type: "photo", media: "attach://image" }]
}
sh(
'curl', 'https://api.telegram.org/botToken/sendMediaGroup',
'--request', 'POST',
'--header', 'Content-Type: application/json',
'--data', data.to_json
)
Doing so ensures that your JSON is always valid. (syntactically)
Related
I have an ElasticSearch index which has a name with . (Example: my_index-2020.11.06-001). When I use SQL to get the count of all documents, I am getting the following error
curl --location --request POST '127.0.0.1:9200/_opendistro/_sql'
--header 'Content-Type: application/json'
--data-raw '{
"query": "SELECT count(*) FROM my_index-2020.11.06-001"
}'
Failed to parse query due to offending symbol [.11] at: 'SELECT count(*) FROM my_index-2020.11.06-001 ...
I have also tried to use backtick (`) and single quotes (') in the index name, that is not helping either
curl --location --request POST '127.0.0.1:9200/_opendistro/_sql'
--header 'Content-Type: application/json'
--data-raw '{
"query": "SELECT count(*) FROM `my_index-2020.11.06-001`"
}'
{
"error": {
"reason": "Invalid SQL query",
"details": "Field [my_index-2020.11.06-001] cannot be found or used here.",
"type": "SemanticAnalysisException"
...
Is there any other way to resolve this issue?
It is a bug but there is a workaround
Disable semantic analyzer
curl --location --request PUT 'http://localhost:9200/_cluster/settings' \
--header 'Content-Type: application/json' \
--data-raw '{
"transient": {
"opendistro.sql.query.analysis.enabled": "false"
}
}'
I have been playing around with a api and I am able to request data and parse the json response.
However, I am using variables in my requests and those are giving me issues.
If the variable contains no spaces then all works great, but if there are spaces then I get failures.
This is how I am doing it
search="string with space"
curl -s 'https://apiurl.com' \
> -d 'search "'$search'";' \
> -H 'user-key: xxxxxxxxxxx' \
> -H 'Accept: application/json'
[
{
"title": "Syntax Error",
"status": 400,
"cause": "Mismatched input, double check your input. Common cause is sending \\\" instead of \"."
}
]
Can anyone explain why this happens?
If I use the string with spaces in the request, not using the variable, then it works no problem.
Classic case of the QUOTATION-MARK-19 virus. :)
Just replace 'search "'$search'";' with 'search "'"$search"'";'
All you were missing was an extra double quote to allow for the variable expansion of $search
search="string with space"
curl -s 'https://apiurl.com' \
> -d 'search "'"$search"'";' \
> -H 'user-key: xxxxxxxxxxx' \
> -H 'Accept: application/json'
[
{
"title": "Syntax Error",
"status": 400,
"cause": "Mismatched input, double check your input. Common cause is sending \\\" instead of \"."
}
]
I'm trying to get a cURL script to input my variables in a --data section. I'm fairly new to this, but it's just inserting the variables names.
The background is this script is called to hit an API in our ticketing system to create a new job. I am finding the ticket that is created has the subject "${DESCRIPTION}" and not "problem description".
#!/bin/bash
# This will log a ticket in Ticketing System
DESCRIPTION='Problem Description'
SUBJECT='Problem Subject'
curl --location --request POST 'https://XXXXX.domain.com/helpdesk/tickets.json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--data '{
"helpdesk_ticket":
{
"description": "${DESCRIPTION}",
"subject": "${SUBJECT}",
"email": "email#domain.com",
"priority": 1,
"status": 2
},
"cc_emails": ""
}'
As per always, got this working just after I posted....
The solution was to wrap the variable in "'".
#!/bin/bash
# This will log a ticket in Ticketing System
DESCRIPTION='Problem Description'
SUBJECT='Problem Subject'
curl --location --request POST 'https://XXXXX.domain.com/helpdesk/tickets.json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--data '{
"helpdesk_ticket":
{
"description": "'"$DESCRIPTION"'",
"subject": "'"$SUBJECT"'",
"email": "email#domain.com",
"priority": 1,
"status": 2
},
"cc_emails": ""
}'
I would like to make cURL/Elasticsearch understand HTTP query parameters passed as normal strings while being url encoded by the command.
If I run this HTTP GET via cURL to submit the query to Elasticsearch:
curl \
-H 'Content-Type: application/json' \
-XGET '127.0.0.1:9200/movies/movie/_search?q=%2Byear%3A%3E1980+%2Btitle%3Astar%20wars&pretty'
Then I am able to retrieve the expected documents.
However if I run this cURL query:
curl \
-H 'Content-Type: application/json' \
--data-urlencode "pretty" \
--data-urlencode "q=+year:>1980 +title:star wars&pretty" \
-XGET '127.0.0.1:9200/movies/movie/_search'
Then I get this error:
{
"error": {
"root_cause": [{
"type": "json_parse_exception",
"reason": "Unrecognized token 'pretty': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput#7856627; line: 1, column: 8]"
}],
"type": "json_parse_exception",
"reason": "Unrecognized token 'pretty': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput#7856627; line: 1, column: 8]"
},
"status": 500
}
I am using:
cURL version 7.47.0 which should understand the command parameter --data-urlencode
Elasticsearch 6.3.1
--data-urlencode will send a POST and URL encode the body. You have to use -G or --get in order to send a GET request & append the data specified with --data-urlencode in the URL :
curl -G -v \
-H 'Content-Type: application/json' \
--data-urlencode "pretty=true" \
--data-urlencode "q=+year:>1980 +title:star wars" \
'127.0.0.1:9200/movies/movie/_search'
Trying to get a curl call to work
Here's the ruby code
msg = "john's message"
#body = %Q|{"body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"#{msg}"}]}]}}|.gsub("'"){"\\'"}
puts #body
curl_call = %Q|curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Bearer xxxx' -d '#{#body}' --url 'https://api.atlassian.com/site/xxxx/conversation/xxxx/message'|
puts curl_call
system(curl_call)
This is the error: sh: 1: Syntax error: Unterminated quoted string
Open to suggestions
When you replace ' by unicode encoded (\u0027), then it works:
.gsub("'", "\u0027")