01 Sep 2021 02:37 PM - last edited on 29 May 2023 01:29 PM by MaciejNeumann
Trying to get python requests to work with Metric Ingest. Can use curl command line and it works .Using header with Content-Type text/plain as shown in documentation. Data is as documented but python gives error . Here is curl part of script.
datap = 'mymetric.connection,dt.entity.host:HOST-xxxxxxxxx,url=8999/connect 1'
header1 = '-H Authorization: Api-Token ' +mToken
header2 = '-H Content-Type: text/plain '
kcurl = 'curl -L -X POST '+apiurl+' '+header1+' '+header2+' --data '+datap
Tried using the following for requests
headers = header1,header2
response = requests.post('https://xxxxxxxxx.live.dynatrace.com/api/v2/metrics/ingest', headers=headers, data=datap)
print(response)
gives error --> AttributeError: 'tuple' object has no attribute 'items'
Solved! Go to Solution.
01 Sep 2021 02:41 PM
Your headers are incorrect. Go with:
headers = {"Authorization": "Api-Token " + token, "Content-Type": "text/plain"}
response = requests.post("https://xxxxxxxxx.live.dynatrace.com/api/v2/metrics/ingest", headers=headers, data="mymetric.connection,dt.entity.host:HOST-xxxxxxxxx,url=8999/connect 1")
Mike
01 Sep 2021 02:57 PM
getting a different error now - i think it has something to do with the url=8999/connect part of the data - do I need to escape the / ?
<Response [400]>
{"linesOk":0,"linesInvalid":1,"error":{"code":400,"message":"1 invalid lines","invalidLines":[{"line":1,"error":"invalid dimension key in message 'mymetric.connection,dt.entity.host:HOST-CED4E27005B91CF6,url=899'; error at 'H' (index 35)"}]},"warnings":null}
01 Sep 2021 03:08 PM
the 400 was due to dt.entity.host:HOST-xxx
should have been
dt.entity.host=HOST-xxx. It works now
got a 202 response
<Response [202]>
{"linesOk":1,"linesInvalid":0,"error":null,"warnings":null}