Trouble sending POST request

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’, 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
");
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.

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.

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:

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:

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>).

@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
',
‘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")

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?

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

@coryo123 I do believe you are god lol that fixed it. Changed it to 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