SusAg Tourism
SusAg Tourism

Reputation: 11

How do I print only the output of a python curl

I am trying to execute this bash command using python and get the output:

This is the command I am using:

cmd_ = "curl -k -X GET https://consul.cicdtest.us-east-1.dev:8543/v1/kv/" +constants.NAMESPACE+"/"+constants.CONSUL_KEY+"?token="+decode_token

I execute above cmd_ as below:

process = Popen(cmd_, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = process.stdout.read()    
print(output)

I get the output as follows (which is working properly):

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   144  100   144    0     0   5142      0 --:--:-- --:--:-- --:--:--  5142

[{"LockIndex":0,"Key":"bitesize-troubleshooter-2/CONSUL_TEST-2","Flags":0,"Value":"dGVzdF9kYXRhLTI=","CreateIndex":338871,"ModifyIndex":341651}]

Only output I need is:

[{"LockIndex":0,"Key":"bitesize-troubleshooter-2/CONSUL_TEST-2","Flags":0,"Value":"dGVzdF9kYXRhLTI=","CreateIndex":338871,"ModifyIndex":341651}]

But somehow I get this header like this:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   144  100   144    0     0   5142      0 --:--:-- --:--:-- --:--:--  5142

How can I remove this header like thing and access only the Value in the output below?

[{"LockIndex":0,"Key":"bitesize-troubleshooter-2/CONSUL_TEST-2","Flags":0,"Value":"dGVzdF9kYXRhLTI=","CreateIndex":338871,"ModifyIndex":341651}]

What I really want from above output is the Value

Upvotes: 0

Views: 1851

Answers (2)

Shambu
Shambu

Reputation: 2832

Since you already have python script, you make the curl call using python. This way you can get the content of the response which is you after and better way than filtering it out from stdout. Below is python3 syntax

import requests

response = requests.get(f"https://consul.cicdtest.us-east-1.dev:8543/v1/kv/{constants.NAMESPACE}/{constants.CONSUL_KEY}?token={decode_token}")
print(response.content)

Below is python2.7 syntax

import requests
url =  "https://consul.cicdtest.us-east-1.dev:8543/v1/kv/" +constants.NAMESPACE+"/"+constants.CONSUL_KEY+"?token="+decode_token
response = requests.get(url)
print response.content

Upvotes: 2

AKX
AKX

Reputation: 169051

curl -s makes Curl silent.

-s, --silent Silent or quiet mode.
Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.

However, the actual problem here is you're setting stderr=STDOUT, so the progress meter, usually printed to the standard error stream, is redirected to the standard output stream you're capturing.

You'll also want to get rid of that.

Upvotes: 1

Related Questions