Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i need to increment the userb and usere but when i write my variable in curl , and when i try it i get nothing
(1..180000).step(20000)do |userb|
(20000..180000).step(20000)do |usere|
curl = %x[ curl -i -s -H "Host: xxxx" "http://XXXXX/scripts/exportStatsCsv/testA1?start='+ userb +'&end='+ usere +'&startDate='#{#array_timestampdate[0]}'&endDate='#{#array_timestampdate[1]}'" ] sleep(10)
end
end
Try running your code with warnings and debugging turned on:
ruby -cW2 path/to/your/code
You should see something like:
syntax error, unexpected tIDENTIFIER, expecting keyword_end
... sleep(10)
... ^
You need to do this as a first step when you run into a problem. Ruby will give you more detailed information about problems with the script when warnings are enabled and set to their highest value. Here's what the flags mean:
-c check syntax only
-W[level=2] set warning level; 0=silence, 1=medium, 2=verbose
You're getting this error, because sleep(10) needs to be executed as a separate statement. You can either insert ; between it, and the call to cURL, or put it on its one line. I'd recommend the second option in order to make the commands easier to read.
Also, I'd highly recommend using the Curb gem instead of launching cURL in a sub-shell like you are. You're losing flexibility and wasting CPU time having Ruby, then the OS, create a new shell to launch cURL.
Finally, you need to learn to write your code more clearly or you'll paint yourself into corners of confusion in no time. Here's a starting point for how I'd write the code:
require 'uri'
#array_timestampdate = ['start_date', 'end_date']
(1..180000).step(20000) do |userb|
(20000..180000).step(20000) do |usere|
uri = URI.parse('http://XXXXX/scripts/exportStatsCsv/testA1')
uri.query = URI.encode_www_form(
{
'start' => userb,
'end' => usere,
'startDate' => #array_timestampdate[0],
'endDate' => #array_timestampdate[1]
}
)
curl = %Q[ curl -i -s -H "Host: xxxx" "#{uri.to_s}" ]
puts curl
end
end
With a little example of the output:
>> curl -i -s -H "Host: xxxx" "http://XXXXX/scripts/exportStatsCsv/testA1?start=1&end=20000&startDate=start_date&endDate=end_date"
...
>> curl -i -s -H "Host: xxxx" "http://XXXXX/scripts/exportStatsCsv/testA1?start=160001&end=180000&startDate=start_date&endDate=end_date"
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to run a bat file that checks the output of curl request and when the condition is met act accordingly.
the curl request in a bat file is as follows:
curl --insecure -X POST https://api.zilliqa.com/ -H "Content-type: application/json" --data "{\"id\":\"1\", \"jsonrpc\":\"2.0\", \"method\":\"GetNumTxBlocks\", \"params\":"[""]"}"
the output of the request is as follows: {"id":"1","jsonrpc":"2.0","result":"5820"}
and the the result is numeric, growing all the time.
As complete newbie i can not figure out how to proceed.
please give me advice, best if you could provide with example as well.
I want the IF statement to react when the last two digits of the result are greater than 95. is that also doable? for now the result is 4 digit number but will grow and be 5 then 6 digit number.
hope it is possible. if not, please suggest a possible solution.
AS curl.exe returns a json string like {"id":"1","jsonrpc":"2.0","result":"5965"}
You can parse with a for /f:
#Echo off
for /f "usebackq tokens=4 delims=:}" %%A in (`
curl --insecure -X POST https://api.zilliqa.com/ -H "Content-type: application/json" --data "{\"id\":\"1\", \"jsonrpc\":\"2.0\", \"method\":\"GetNumTxBlocks\", \"params\":"[""]"}"
`) do set "result=%%~A"
Echo result=%result%
To calculate the last two digits do a modulus division by 100 and compare with an if
Set /A "Last2Digits=result %% 100"
if %Last2Digits% gtr 95 (Echo greater 95) else ( echo less or equal 95)
Or do it in PowerShell:
$result = (curl.exe --% --insecure -X POST https://api.zilliqa.com/ -H "Content-type: application/json" --data "{\"id\":\"1\", \"jsonrpc\":\"2.0\", \"method\":\"GetNumTxBlocks\", \"params\":"[""]"}"|ConvertFrom-json).result
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
EDIT: Solved, typo. Smart me \ + " " does work (backslash and space do work)
I know this has been asked before and I want to reference this post, where the - to me correct response - is using backslash.
Unfortunately, I do not know how. I tried a multiline curl request, and the following two examples made me post here, coz none of them worked:
Take 1:
curl -X PUT localhost:8080/employees/4\
-H 'Content-type:application/json'\
-d '{"name:" "Smurf", "role": "Blueman"}'
{"timestamp":"2019-01-08T13:06:36.563+0000","status":400,"error":"Bad Request","message":"Required request body is missing: payroll.Employee payroll.EmployeeController.replaceEmployee(payroll.Employee,java.lang.Long)","path":"/employees/4-H"}curl: (3) Port number ended with 'a'
[1/2]: "name:" "Smurf" --> <stdout>
--_curl_--"name:" "Smurf"
curl: (3) Port number ended with '"'
[2/2]: "role": "Blueman" --> <stdout>
--_curl_-- "role": "Blueman"
curl: (3) Port number ended with ' '
So I figured, adding space before using the backslash would solve this.
But then this happened:
curl -X PUT localhost:8080/employees/4 \
-H 'Content-type:application/json' \
-d '{"name:" "Smurf", "role": "Blueman"}'
{"timestamp":"2019-01-08T13:07:32.204+0000","status":400,"error":"Bad Request","message":"JSON parse error: Unexpected character ('\"' (code 34)): was expecting a colon to separate field name and value; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('\"' (code 34)): was expecting a colon to separate field name and value\n at [Source: (PushbackInputStream); line: 1, column: 11]","path":"/employees/4"}%
so now it doesn't accept \ as the newline terminal syntax, but tries to interept it.
What am I missing?
You have a typo. Try with:
curl -X PUT localhost:8080/employees/4 \
-H 'Content-type:application/json' \
-d '{"name": "Smurf", "role": "Blueman"}'
The error message tells you exactly which is the problem.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to check is my website online or not using BASH script.
Currently i am trying to check if curl response error then restart APACHE but its not working because i am using cloud flare. I am not a BASH script developer. I need help to make it possible to check JSON response from my URL if its true then OKAY otherwise restart Apache.
URL="http://apental-calc.com/online.php"
for i in {1..5};do
curl $URL >/dev/null 2>&1
if [[ $? -ne 0 ]];then
echo "Restarting Apache2"
service apache2 restart
sleep 5
continue
else
echo "Working"
exit 0
fi
done
I guess what you are trying to recover is a HTTP Code stating that the request was successful, in that case you can try this:
HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" $URL)
This will print the HTTP status of the request done, for instance:
HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://www.google.com)
echo $HTTP_RESPONSE
301
200 and 30X codes indicate a working server. If you don´t get result, then it is time to check CURL exit code:
if [ $? -eq 7 ] then ...
Being 7 the code returned when CURL can not connect to the host:
Failed to connect to host. curl managed to get an IP address to the machine and it tried to setup a TCP connection to the host but failed. This can be because you have specified the wrong port number, entered the wrong host name, the wrong protocol or perhaps because there is a firewall or another network equipment in between that blocks the traffic from getting through
See all codes:
https://ec.haxx.se/usingcurl-returns.html
UPDATE: As discussed in the comments, the question is not clear on whether the check is to be done in the HTTP Status code or in a JSON payload (which is missing). In case you want to check the payload, this can be used:
URL Serving a dummy JSON:
https://jsonplaceholder.typicode.com/todos/2
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
}
To parse this, this can be used (let´s pretend we want to check the "title"):
$ curl -s https://jsonplaceholder.typicode.com/todos/2 | grep -Po '"title": ".*' | cut -f 4 -d '"'
This matches only the specified pattern and extracts the value.
Setup
I'm Using curl in the following bash script to push a JSON file to a REST API running in tomcat sitting behind nginx.
while IFS= read -d '' -r file; do
base=$(basename "$file")
datetime=$(find $file -maxdepth 0 -printf "%TY/%Tm/%Td %TH:%TM:%.2TS")
curl -vX POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" \
-d #"$file" -u vangeeij:eian12 \
"http://192.168.105.10/homeaccess/services/aCStats/uploadData?username=vangeeij&filename=$base&datetime=$datetime"
#sudo mv "$file" /home/vangeeij/acserver/resultsOld
done < <(sudo find . -type f -print0)
Problem
When running this script I get a http 400 response with curl error:
curl: (18) transfer closed with outstanding read data remaining
What I have tried
I have found 2 things. First running the same URL and body through Postman yields a successful POST.
I found that this error goes away when the last parameter is removed from the URL &datetime=$datetime
I have also found a few connections between this error and setting a curl option something like
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
But I'm not sure where/how to set this exactly when using curl in a simple bash script
Question
What do I need to change in my curl command to get rid of the error and still be able to use all parameters?
UPDATE
Starting a new question, as further investigation has lead me to a better understanding of the problem.
New Question Link
The error has to do with the fact that the parameter datetime= ends up with text in it that needs to be URL encoded.
This was confirmed by replacing the variable with 2017%2F03%2F01%2008%3A50%3A56
and it worked.
So now the problem is, that I can't get --data-urlencode datetime=$datetime to work. It seems this just gets appended to the JSON data or something.
This error is being generated by the fact that the datetime= paramater is being passed in with non encoded non URL friendly characters... (eg. space).
The fix to this would be to find a way to convert the $datetime to a URLEncoded String.
eg. convert:
2017/03/01 08:50:56
TO
2017%2F03%2F01%2008%3A50%3A56
See the following discussion for one method to accomplish this.
Post JSON data to Rest with URLEncoded query paramaters
Follow the instruction in Paypal Developer 'make your first call':
curl https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
It is working and get the expected result like the instruction states, but I prefer to use ruby curb gem:
require 'curl'
paypal_result = Curl::Easy.http_post("https://api.sandbox.paypal.com/v1/oauth2/token", "grant_type=client_credentials") do |http|
http.headers['Accept'] = "application/json"
http.headers['Accept-Language'] = "en_US"
http.username = "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp"
end
puts paypal_result.body_str
However I got the following:
{"error":"invalid_client","error_description":"The basic auth authorization header cannot be decoded"}
It is an error for sure, but what's wrong with my curb syntax? Any idea?
I can't tell you exactly what's wrong, but I can tell you how to see what's happening. Try putting it into verbose mode, so you can see what data and headers are actually being sent:
curl = Curl::Easy.new
# Make it verbose, prints to stderr
curl.verbose = true
paypal_result = curl.http_post("https://api.sandbox.paypal.com/v1/oauth2/token", "grant_type=client_credentials") do |http|
# etc.
end
You can compare with the CLI version by using the -v flag.
Hope that helps you discover the difference.