# Ingest API using python script

**URL:** <https://community.dynatrace.com/t/ingest-api-using-python-script/32572>\
**Category:** Dynatrace API\
**Tags:** dynatrace-managed, dynatrace-api\
**Created:** [September 1, 2021, 1:37pm UTC](https://community.dynatrace.com/t/ingest-api-using-python-script/32572 "2021-09-01T13:37:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![curtis\_kellogg](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@curtis\_kellogg](https://community.dynatrace.com/u/curtis_kellogg)\
**Post date:** [September 1, 2021, 1:37pm UTC](https://community.dynatrace.com/t/ingest-api-using-python-script/32572/1 "2021-09-01T13:37:50Z")

</div>

Trying to get python requests to work with Metric Ingest.&nbsp; Can use curl command line and it works .Using header with Content-Type text/plain as shown in documentation.&nbsp; Data is as documented but python gives error .&nbsp; &nbsp;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](https://xxxxxxxxx.live.dynatrace.com/api/v2/metrics/ingest)’, headers=headers, data=datap)

print(response)

gives error&nbsp; --\>&nbsp;AttributeError: ‘tuple’ object has no attribute ‘items’

---

<div class="post-metadata">

**Author:** ![Mike\_L](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mike_l/32/11717_2.png) [@Mike\_L](https://community.dynatrace.com/u/Mike_L)\
**Post date:** [September 1, 2021, 1:41pm UTC](https://community.dynatrace.com/t/ingest-api-using-python-script/32572/2 "2021-09-01T13:41:29Z")

</div>

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](https://xxxxxxxxx.live.dynatrace.com/api/v2/metrics/ingest)”, headers=headers, data=“mymetric.connection,dt.entity.host:HOST-xxxxxxxxx,url=8999/connect 1”)

Mike

---

<div class="post-metadata">

**Author:** ![curtis\_kellogg](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@curtis\_kellogg](https://community.dynatrace.com/u/curtis_kellogg)\
**Post date:** [September 1, 2021, 1:57pm UTC](https://community.dynatrace.com/t/ingest-api-using-python-script/32572/3 "2021-09-01T13:57:17Z")

</div>

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}

---

<div class="post-metadata">

**Author:** ![curtis\_kellogg](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@curtis\_kellogg](https://community.dynatrace.com/u/curtis_kellogg)\
**Post date:** [September 1, 2021, 2:08pm UTC](https://community.dynatrace.com/t/ingest-api-using-python-script/32572/4 "2021-09-01T14:08:37Z")

</div>

the 400 was due to dt.entity.host:HOST-xxx

should have been

dt.entity.host=HOST-xxx.&nbsp; It works now

got a 202 response

\<Response [202]\>  
{“linesOk”:1,“linesInvalid”:0,“error”:null,“warnings”:null}
