Skip to content

Commit

Permalink
feat: add support for sending non-form strings as POST bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
sesh committed Nov 26, 2022
1 parent 14d4e03 commit 965d708
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -83,6 +83,7 @@ Run `black` before committing any changes.
## Packaging for release

```
rm dist/*
python3 -m build
python3 -m twine upload dist/*
```
2 changes: 1 addition & 1 deletion setup.cfg
@@ -1,6 +1,6 @@
[metadata]
name = thttp
version = 1.0.0
version = 1.1.0
author = Brenton Cleeland
author_email = brenton@brntn.me
description = An incredibly lightweight wrapper around urllib
Expand Down
18 changes: 14 additions & 4 deletions thttp.py
Expand Up @@ -76,8 +76,10 @@ def request(
if json: # if we have json, dump it to a string and put it in our data variable
headers["content-type"] = "application/json"
data = json_lib.dumps(json).encode("utf-8")
elif data:
elif data and not isinstance(data, (str, bytes)):
data = urlencode(data).encode()
elif isinstance(data, str):
data = data.encode()

if basic_auth and len(basic_auth) == 2 and "authorization" not in headers:
username, password = basic_auth
Expand Down Expand Up @@ -206,15 +208,15 @@ def test_should_form_encode_non_json_post_requests(self):
def test_should_follow_redirect(self):
response = request(
"https://httpbingo.org/redirect-to",
params={"url": "https://duckduckgo.com/"},
params={"url": "https://example.org/"},
)
self.assertEqual(response.url, "https://duckduckgo.com/")
self.assertEqual(response.url, "https://example.org/")
self.assertEqual(response.status, 200)

def test_should_not_follow_redirect_if_redirect_false(self):
response = request(
"https://httpbingo.org/redirect-to",
params={"url": "https://duckduckgo.com/"},
params={"url": "https://example.org/"},
redirect=False,
)
self.assertEqual(response.status, 302)
Expand Down Expand Up @@ -257,3 +259,11 @@ def test_should_timeout(self):
def test_should_handle_head_requests(self):
response = request("http://httpbingo.org/head", method="HEAD")
self.assertTrue(response.content == b"")

def test_should_post_data_string(self):
response = request(
"https://ntfy.sh/thttp-test-ntfy",
data="The thttp test suite was executed!",
method="POST",
)
self.assertTrue(response.json["topic"] == "thttp-test-ntfy")

0 comments on commit 965d708

Please sign in to comment.