Skip to content

Commit

Permalink
improve requrl_click
Browse files Browse the repository at this point in the history
  • Loading branch information
LoicGrobol committed Dec 19, 2023
1 parent 64c2190 commit 98183f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 9 additions & 2 deletions slides/04-requests/requests.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,21 @@ En fait, `requests` ne sait passer que des paramètres binaires, et il encode im
Pour utiliser un autre encodage, il faut le faire à la main.

```python
response = requests.post("https://httpbin.org/post", data="We are the knights who say “Ni”!".encode("utf-8"))
response = requests.post(
"https://httpbin.org/post",
data="We are the knights who say “Ni”!".encode("utf-8"),
)
print(response.text)
```

Mais là le serveur ne saura pas deviner que c'est cet encodage que vous utilisez, il faudra encore lui dire via les *headers*.

```python
response = requests.post("https://httpbin.org/post", data="We are the knights who say “Ni”!".encode("utf-8"), headers={'Content-Type': 'text/plain; charset=utf-8'})
response = requests.post(
"https://httpbin.org/post",
data="We are the knights who say “Ni”!".encode("utf-8"),
headers={"Content-Type": "text/plain; charset=utf-8"},
)
print(response.text)
```

Expand Down
4 changes: 1 addition & 3 deletions slides/04-requests/requrl_click.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import argparse

import click
import requests


@click.command("Download a text resource and print it")
@click.command(help="Download a text resource and print it")
@click.argument("url")
def main(url):
response = requests.get(url)
Expand Down

0 comments on commit 98183f5

Please sign in to comment.