# Trouble sending POST request

**URL:** <https://forums.plex.tv/t/trouble-sending-post-request/140283>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [March 27, 2016, 2:16pm UTC](https://forums.plex.tv/t/trouble-sending-post-request/140283 "2016-03-27T14:16:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xtv302](https://avatars.discourse-cdn.com/v4/letter/x/3ab097/32.png) [@xtv302](https://forums.plex.tv/u/xtv302)\
**Post date:** [March 27, 2016, 2:16pm UTC](https://forums.plex.tv/t/trouble-sending-post-request/140283/1 "2016-03-27T14:16:09Z")

</div>

I have been trying to figure out how to do this in python but not having much luck. The code I have is in php

`$connection = fsockopen(‘[some.website.net](http://some.website.net)’, 80);  
fputs($connection, "POST /v1/list\_all HTTP/1.1  
");  
fputs($connection, "User-Agent: USER-AGENT-SOME-APP-V1  
");  
fputs($connection, "app-token: a44a26bc9d2a6f7889a65247e0f42f8a  
");  
fputs($connection, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8  
");  
fputs($connection, "Host: [some.website.net](http://some.website.net)  
");  
fputs($connection, "Connection: Keep-Alive  
");  
fputs($connection, "Accept-Encoding: identity  
");  
fputs($connection, "Content-Length: 21  
");  
fputs($connection, "Connection: close

");  
fputs($connection, “username=MYUSERNAME”);  
$response = stream\_get\_contents($connection);`

I have read that most people use the requests module but it’s not included with plex, is there another way to do this? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Twoure](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/twoure/32/91016_2.png) [@Twoure](https://forums.plex.tv/u/Twoure)\
**Post date:** [March 27, 2016, 2:52pm UTC](https://forums.plex.tv/t/trouble-sending-post-request/140283/2 "2016-03-27T14:52:19Z")

</div>

By default if you include the `data` field within the `HTTP.Request` it will do a POST. There is also a `method` field that you can populate to use PUT and other methods. Most of what you have above seems to be headers, they can be passed as a dictionary.

```auto
HTTP.Request(url, data=d, headers=h, method=m) 

```

~~Not 100% on the name of the _method_ field (without double checking) but know it takes a string value.~~  
_method_ field takes a string value.

* * *

**Edit:** double checked the _method_ name, and is correct.

Did some digging and found the following:

```auto
HTTP.Request(url, values=None, headers={}, cacheTime=None, encoding=None, errors=None, timeout=GLOBAL_DEFAULT_TIMEOUT, immediate=False, sleep=0, data=None, follow_redirects=True, method=None)

Plist.ObjectFromURL(url, values=None, headers={}, cacheTime=None, encoding=None, errors=None, timeout=GLOBAL_DEFAULT_TIMEOUT, sleep=0, follow_redirects=True, method=None, max_size=None)

JSON.ObjectFromURL(url, values=None, headers={}, cacheTime=None, encoding=None, errors=None, timeout=GLOBAL_DEFAULT_TIMEOUT, sleep=0, follow_redirects=True, method=None, max_size=None)

RSS.FeedFromURL(url, values=None, headers={}, cacheTime=None, encoding=None, errors=None, timeout=GLOBAL_DEFAULT_TIMEOUT, sleep=0, follow_redirects=True, method=None, max_size=None)

YAML.ObjectFromURL(url, values=None, headers={}, cacheTime=None, encoding=None, errors=None, timeout=GLOBAL_DEFAULT_TIMEOUT, sleep=0, follow_redirects=True, method=None, max_size=None)

XML.ElementFromURL(url, values=None, headers={}, cacheTime=None, encoding=None, errors=None, timeout=GLOBAL_DEFAULT_TIMEOUT, sleep=0, follow_redirects=True, method=None, max_size=None)

HTML.ElementFromURL(url, values=None, headers={}, cacheTime=None, encoding=None, errors=None, timeout=GLOBAL_DEFAULT_TIMEOUT, sleep=0, follow_redirects=True, method=None, max_size=None)

```

Each of the above can issue a POST when `method = "POST"` and use the `values` field to pass URL encoded content for the POST request. The `values` field takes a `dict`. The above functions were taken from the `parsekit.py` and `networkkit.py` files, and show their default input. Files located here:

```auto
Plug-ins-cece46d/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/api/parsekit.py

Plug-ins-cece46d/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/api/networkkit.py

```

* * *

**Notes:**

1. `HTTP.Request()` function is the only one with a `data` field. Also you cannot use the `data` and `values` field within the same request.
2. I Have not tested any of the above except for the `HTTP.Request()` as previously described.
3. For full explanation of each function above, refer to their functions within their respective files. Each are documented with value types and intended functionality.
4. `Plug-ins-cece46d` is specific to PMS v0.9.16.3. `<hash>` depends on current Plex Media Server (PMS) version, (where `<hash>` is `Plug-ins-<hash>`).

---

<div class="post-metadata">

**Author:** ![xtv302](https://avatars.discourse-cdn.com/v4/letter/x/3ab097/32.png) [@xtv302](https://forums.plex.tv/u/xtv302)\
**Post date:** [March 27, 2016, 2:59pm UTC](https://forums.plex.tv/t/trouble-sending-post-request/140283/3 "2016-03-27T14:59:12Z")

</div>

@Twoure Thanks. I’ll give this a try.

Edit: Seems that everything is working as it should but I am not getting the json returned as the php code does. I believe the problem is within the username part.

fputs($connection, “username=MYUSERNAME”);

‘username’: ‘MYUSERNAME’

I get a error if using ‘username=MYUSERNAME’ and error if using ‘’: ‘username=MYUSERNAME’

Not entirely sure how to send that bit. Here is the code I am using

```
    dict = {'POST': '/v1/list_all HTTP/1.1

```

',  
‘User-Agent’: 'USER-AGENT-SOME-APP-V1  
',  
‘app-token’: 'a44a26bc9d2a6f7889a65247e0f42f8a  
',  
‘Content-Type’: 'application/x-www-form-urlencoded; charset=UTF-8  
',  
‘Host’: '[some.website.net](http://some.website.net)  
',  
‘Connection’: 'Keep-Alive  
',  
‘Accept-Encoding’: 'identity  
',  
‘Content-Length’: '21  
',  
‘Connection’: 'close

',  
‘username’: ‘MYUSERNAME’};

```
    json_list = HTTP.Request("http://some.website.net", headers=dict, method="POST")
```

---

<div class="post-metadata">

**Author:** ![coryo123](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@coryo123](https://forums.plex.tv/u/coryo123)\
**Post date:** [March 27, 2016, 4:17pm UTC](https://forums.plex.tv/t/trouble-sending-post-request/140283/4 "2016-03-27T16:17:26Z")

</div>

you don’t need the `\r` and `` in your headers.

also are you trying to request `http://some.website.net` or `http://some.website.net/v1/list_all`?

```auto
dict = {
    'User-Agent': 'USER-AGENT-SOME-APP-V1', 
    'app-token': 'a44a26bc9d2a6f7889a65247e0f42f8a',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Host': 'some.website.net',
    'Connection': 'Keep-Alive',
    'Accept-Encoding': 'identity',
    'Content-Length': '21',
    'Connection': 'close',
    'username': 'MYUSERNAME'
}

res = HTTP.Request("http://some.website.net/v1/list_all", headers=dict, method="POST")
json_list = res.content

```

---

<div class="post-metadata">

**Author:** ![xtv302](https://avatars.discourse-cdn.com/v4/letter/x/3ab097/32.png) [@xtv302](https://forums.plex.tv/u/xtv302)\
**Post date:** [March 27, 2016, 4:28pm UTC](https://forums.plex.tv/t/trouble-sending-post-request/140283/5 "2016-03-27T16:28:03Z")

</div>

@coryo123 I do believe you are god lol that fixed it. Changed it to [http://some.website.net/v1/list\_all](http://some.website.net/v1/list_all) and removed the \r  
. Had to add the line data=‘username=MYUSERNAME’ in http.request. Works perfect and returns the json as it should. Many thanks

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [January 8, 2020, 7:56pm UTC](https://forums.plex.tv/t/trouble-sending-post-request/140283/6 "2020-01-08T19:56:35Z")

</div>


