cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Ingest API using python script

curtis_kellogg
Participant

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'

3 REPLIES 3

Mike_L
Dynatrace Pro
Dynatrace Pro

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

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}

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}