# Problems with Talk2M DMWeb API through system.net.httpClient and system.net.httpPost

**URL:** <https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441>\
**Category:** Ignition\
**Tags:** ignition80, scripting\
**Created:** [November 9, 2020, 11:25am UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441 "2020-11-09T11:25:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![betti.francesco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/betti.francesco/32/6309_2.png) [@betti.francesco](https://forum.inductiveautomation.com/u/betti.francesco)\
**Post date:** [November 9, 2020, 11:25am UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/1 "2020-11-09T11:25:24Z")

</div>

Hello, I’m trying to use eWon’s Talk2M DMWeb API to access to data stored in the cloud. Basically, I’m just testing the ‘getewons’ method, which requires two params, the developer id and Talk2m token, inside the body request (set as x-www-form-urlencoded).  
Using Postman I get this result

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/c/1c945f492fb78c9890c792f275b13e002e3b3f11.png)  
which is good, because actualy I have not eWon registered on the Talk2M account.  
Using Python, with the `requests` library I get the same result.

Using Ignition 8.1 rc-3, I tried to use system.net.httpClient, here’s the code:

```python
url = "https://data.talk2m.com/getewons"
devId = "6de9e..."
t2mtoken = "pkv8..."

data={"t2mdevid":devId,"t2mtoken":t2mtoken}
headers = {'Content-Type': "application/x-www-form-urlencoded"}
http = system.net.httpClient()
http.post(url=url, data=data, headers=headers).json

```

And I got this output

```json
{'code': 400L, 'message': u"Missing argument 't2mdevid' for method parameter of type String", 'success': False}

```

which is the same result I get if I don’t pass the ‘data’ parameter.

I also tried with the system.net.httpPost method, in it’s two forms:

```python
system.net.httpPost(url=url, postParams=data) 
system.net.httpPost(url=url, postData=str(data), headerValues=headers)

```

In both cases I’m getting this error:

```auto
IOError: Server returned HTTP response code: 400 for URL: https://data.talk2m.com/getewons

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![rbabcock](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rbabcock/32/13425_2.png) [@rbabcock](https://forum.inductiveautomation.com/u/rbabcock)\
**Post date:** [November 9, 2020, 12:06pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/2 "2020-11-09T12:06:49Z")

</div>

Have you tried using “application/json;charset=UTF-8” as your content type? It seems to me like your URL isn’t being encoded correctly with the parameters.

---

<div class="post-metadata">

**Author:** ![betti.francesco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/betti.francesco/32/6309_2.png) [@betti.francesco](https://forum.inductiveautomation.com/u/betti.francesco)\
**Post date:** [November 9, 2020, 12:57pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/3 "2020-11-09T12:57:00Z")

</div>

I just tried and I got the same error.

---

<div class="post-metadata">

**Author:** ![rbabcock](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rbabcock/32/13425_2.png) [@rbabcock](https://forum.inductiveautomation.com/u/rbabcock)\
**Post date:** [November 9, 2020, 1:04pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/4 "2020-11-09T13:04:42Z")

</div>

Hm, Maybe try sending your data dictionary as params, instead? I’m guessing at this point, though. I’m not familiar with that particular API.

---

<div class="post-metadata">

**Author:** ![betti.francesco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/betti.francesco/32/6309_2.png) [@betti.francesco](https://forum.inductiveautomation.com/u/betti.francesco)\
**Post date:** [November 9, 2020, 1:40pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/5 "2020-11-09T13:40:11Z")

</div>

The API specificies that those two params has to be sent through the request’s body specifying the x-www-form-urlencoded content type, in fact if I try to send them as params with Postman I got another error from the server: “URL query strings are considered unsafe and not allowed when using Token authentication. Put all your parameters in the POST body.”

Anyway, I tried to run a post from Ignition to the postman-echo server and it works, so I gues it’s a DMWeb server side problem. I will try again when I’ll have some eWon attached to the Talk2M account.

@rbabcock thanks for the help! 🙂

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [November 9, 2020, 2:15pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/6 "2020-11-09T14:15:37Z")

</div>

If your content type is really supposed to be “x-www-form-urlencoded” then your body would be a string that looks like this:

```auto
"devId=6de9e...&t2mtoken=pkv8..."

```

and not a JSON object.

---

<div class="post-metadata">

**Author:** ![betti.francesco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/betti.francesco/32/6309_2.png) [@betti.francesco](https://forum.inductiveautomation.com/u/betti.francesco)\
**Post date:** [November 9, 2020, 2:37pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/7 "2020-11-09T14:37:48Z")

</div>

Right! I forgot to encode the data object -\_- This will solve the problem when using the httpClient:

```python
import urllib
data=urllib.urlencode({"t2mdevid":devId,"t2mtoken":t2mtoken})

```

Anyway, I can’t get the httpPost to work

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 9, 2020, 4:43pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/8 "2020-11-09T16:43:12Z")

</div>

Adding better form submission to httpClient is on the list, but for now, yeah, you have to do it pretty ‘manually’.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [October 16, 2023, 6:17am UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/9 "2023-10-16T06:17:49Z")

</div>

I'll pipe in here, 3 years later, and say that if you have a more complex structure, you can't use `urlencode`, for example (example data to submit metrics to DataDog):

```auto
{
  "series": [
    {
      "metric": "system.load.1",
      "type": 0,
      "points": [
        {
          "timestamp": 1636629071,
          "value": 0.7
        }
      ],
      "resources": [
        {
          "name": "dummyhost",
          "type": "host"
        }
      ]
    }
  ]
}

```

For this you can just jsonEncode the data and pass it into post directly (tbh I'd probably just do this for simple dicts as well):

```auto
data={
  "series": [
    {
      "metric": "system.load.1",
      "type": 0,
      "points": [
        {
          "timestamp": 1636629071,
          "value": 0.7
        }
      ],
      "resources": [
        {
          "name": "dummyhost",
          "type": "host"
        }
      ]
    }
  ]
}
data_json = system.util.jsonEncode(data)
http = system.net.httpClient()
response = http.post(url=api_url, data=data_json, headers=headers)
if not response.good:
   print 'Who let the DataDogs out??'

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 23, 2023, 4:33pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/10 "2023-10-23T16:33:47Z")

</div>

Posterity: If you're just passing **JSON** , and you want it be encoded as such over the wire, you can (and should) omit `system.util.jsonEncode` entirely and just pass your dictionary in as the data argument; `system.net.httpClient` will encode it for you and set the appropriate content-type header.

The `urlencode` stuff mentioned upstream should only be necessary for `form-encoded` POST data, a distinct content-type with its own encoding rules that `httpClient` has no special handling for, for now.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [October 23, 2023, 7:43pm UTC](https://forum.inductiveautomation.com/t/problems-with-talk2m-dmweb-api-through-system-net-httpclient-and-system-net-httppost/40441/11 "2023-10-23T19:43:06Z")

</div>

Ah, nice. I think I was setting the content-type in my headers manually like a dope 🤦
