Using output from curl post with IF ELSE statements [closed] - windows

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

Related

How to for loop CURL commands?

I am using Watson's Speech-To-Text Lite service and I am trying to find a way to automate the loading of new audio files to transcribe. I am very new to Bash and so I'm unclear of even the more rudimentary terms - so I'm finding the problem hard to find a solution for.
For a single use-case, I run the following file (my API key omitted with 'MY APIKEY')
curl -X POST -u "apikey: MY APIKEY" --header "Content-Type: audio/flac" --data-binary "#audiofile_1.flac" "https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_BroadbandModel&speaker_labels=true" > C:/Users/outputpath/output_1.txt
What I am essentially trying to achieve is to overcome having to manually type and retype the names of the audio files and output. So if I had three (or more) audio files (i.e. audiofile_1, 2, and 3.flac), i would like to create an output file corresponding to each audio file - Some psuedo-code that might help explain what I mean would be
files = [file_1, file_2, file_3]
for file_x in files:
run curl command
save as output_x
You almost got it. You just need to learn some shell syntax:
files=("file_1" "file_2" "file_3")
for file_x in "${files[#]}"
do
curl -X POST -u "apikey: MY APIKEY" --header "Content-Type: audio/flac" --data-binary "#${file_x}" "https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_BroadbandModel&speaker_labels=true" > "C:/Users/outputpath/${file_x}.txt"
done
First you create the files array with your list of files. Then, you iterate over those files and run the curl command on each of them.

Terminal Input over several lines [closed]

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.

Update to-many association

Having a many-to-many relationship between users and groups. I would like to know how to update this relationship with SDR. This is what I've tried so far after reading the docs.
curl -X POST -H 'Content-Type: text/uri-list' -d 'http://localhost:8080/rest/users/5' http://localhost:8080/rest/groups/1/users
Expected result: Add user 5 to group 1.
Actual result: 405 Method Not Allowed.
curl -X PUT -H 'Content-Type: text/uri-list' -d 'http://localhost:8080/rest/users/5' http://localhost:8080/rest/groups/1/users
Expected result: Replace all members of group 1 with user 5.
Actual result: Works as expected.
curl -X PUT -H 'Content-Type: text/uri-list' -d #members.txt http://localhost:8080/rest/groups/1/users
Where the file members.txt has:
http://localhost:8080/rest/users/5
http://localhost:8080/rest/users/6
http://localhost:8080/rest/users/7
Expected result: Replace all members of group 1 with the users 5, 6 and 7.
Actual result: Only last user (in this case 7) gets added.
Could someone provide an example on how to ADD a single URI to an association?. Also if possible, how to add or replace an association with multiple URIs?
After re-reading the documentation, it does indeed say POST should add to the collection.
My experience has been to use PATCH to add to the collection.
To further the answer: You should be able to use PUT CONTENT-TYPE: text/uri-list with a content body having multiple URIs. Each URI is separated by a line break "\n"
Try this:
curl -v -X POST -H "Content-Type: text/uri-list" -d "http://localhost:8080/rest/users/5" http://localhost:8080/rest/groups/1/users

Is it possible to permanently update the value of a TeamCity build parameter as a result of a custom run?

Is it possible to permanently update the value of a build parameter as a result of a custom run?
For example, consider a build which is configured to have the build number format:
%Major%.%Minor%.%Patch%.%build.counter%
Major, Minor and Patch and defined in the build configuration to have certain values. For the sake of an example, lets say this gives a build number of 3.1.2.36.
It is possible to change the build number by clicking '...' next to run and then changing the value of one of the params. Changing Minor from 1->2 and patch from 2->0 would give the next build the number 3.2.0.37.
I'm not overly concerned that 37 hasn't be reset to 0, but the problem is that the next build which is triggered (not as a result of a custom run) will have the build number 3.1.2.38 which is a lower number. Is it possible that when you run a custom build and change the numbers that the new values are persisted?
I am looking for a way that users with no TeamCity admin rights can cause the version number to be incremented according to the changes they have made.
We are running v8.1.2 (build 29993).
To fix the issue I used the TeamCity REST API. I created a new build param of type prompt called 'ReleaseType' which can be either Patch, Minor or Major. This is then used in this command line script which is set up as a TeamCity build step:
IF "%ReleaseType%"=="Major" (
set /a newVersion=%VersionMajor%+1
curl -v --request PUT -d 0 --Header "Content-Type: text/plain" http://username:password#servername:8080/httpAuth/app/rest/projects/%system.teamcity.projectName%/parameters/VersionMinor
curl -v --request PUT -d 0 --Header "Content-Type: text/plain" http://username:password#servername:8080/httpAuth/app/rest/projects/%system.teamcity.projectName%/parameters/VersionPatch
)
IF "%ReleaseType%"=="Minor" (
set /a newVersion=%VersionMinor%+1
curl -v --request PUT -d 0 --Header "Content-Type: text/plain" http://username:password#servername:8080/httpAuth/app/rest/projects/%system.teamcity.projectName%/parameters/VersionPatch
)
IF "%ReleaseType%"=="Patch" (
set /a newVersion=%VersionPatch%+1
)
curl -v --request PUT -d %%newVersion%% --Header "Content-Type: text/plain" http://username:password#servername:8080/httpAuth/app/rest/projects/%system.teamcity.projectName%/parameters/Version%ReleaseType%
curl -v --request PUT -d 0 --Header "Content-Type: text/plain" http://username:password#servername:8080/httpAuth/app/rest/buildTypes/id:%dep.Dependant_BuildName.system.teamcity.buildType.id%/settings/buildNumberCounter
This increments the specified build number and resets downstream version parts to 0.
For example, a minor version increase on 3.2.12.122 goes to 3.3.0.0.
Note - in my particular example above the build counter is reset on a dependant build and not the configuration which is running. This may or may not be what you are after. Replace
%dep.Dependant_BuildName.system.teamcity.buildType.id%
with
%system.teamcity.buildType.id%
if you want to reset the current running build configuration.

print params each in curl [closed]

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"

Resources