簡體   English   中英

Pass the variable to curl command to inovoke rest api url n python

[英]Pass the variable to curl command to inovoke rest api url n python

我正在嘗試使用變量 triggername 傳遞報告標題名稱。 當我執行代碼時,我在 api url 中沒有捕獲到 triggername 的值。

有人可以幫我解決這個問題嗎?

**#!/usr/bin/python2.7
import requests
import os
import commands
triggername="GSD_Trigger_Active"
print("**********",triggername, type(triggername))
cmd="""curl -v -u $(ops_unv_cred_user_35ab1e1285be4e659f9ab15fca0a35fc):$(ops_unv_cred_pwd_35ab1e1285be4e659f9ab15fca0a35fc)  -H "Accept: application/json" -X GET 'https://xxxxx.cloud/resources/report/run?reporttitle=triggername'
"""
os.system(cmd)

我收到以下錯誤消息:

Could not find report with name "triggername"

認為您需要對變量進行插值。 最簡單的方法就是這個。

triggername = "GSD_Trigger_Active"
cmd= f'curl -v -u $(ops_unv_cred_user_35ab1e1285be4e659f9ab15fca0a35fc):$(ops_unv_cred_pwd_35ab1e1285be4e659f9ab15fca0a35fc)  -H "Accept: application/json" -X GET "https://xxxxx.cloud/resources/report/run?reporttitle={triggername}"'

This should give you the cmd as 'curl -v -u $(ops_unv_cred_user_35ab1e1285be4e659f9ab15fca0a35fc):$(ops_unv_cred_pwd_35ab1e1285be4e659f9ab15fca0a35fc) -H "Accept: application/json" -X GET "https://xxxxx.cloud/resources/report/run?reporttitle=GSD_Trigger_Active"'

添加截圖:

PyShell

python3.6之前使用這個:

cmd = 'curl -v -u $(ops_unv_cred_user_35ab1e1285be4e659f9ab15fca0a35fc):$(ops_unv_cred_pwd_35ab1e1285be4e659f9ab15fca0a35fc)  -H "Accept: application/json" -X GET "https://xxxxx.cloud/resources/report/run?reporttitle={}"'.format(triggername)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM