master
Denis MIGDAL 2 years ago
parent 27202198c4
commit 3b8ac0c471

@ -101,4 +101,75 @@ for name in $ELEM.dataset:
📖 [Plus de méthodes disponibles dans la documentation.](https://developer.mozilla.org/en-US/docs/Web/API/Element)
# Envoyer une requête REST
```javascript
// [JS] Javascript
async query() {
const anwser = fetch($URL);
// ou
const anwser = fetch($URL, {method: "POST", body: $PARAMS);
// [🚩] TODO: tester mode: "no-cors"
const json = await anwser.json(); // récupérer du JSON
const text = await anwser.text(); // récupérer du texte
}
query();
```
```python
# [🐍] Python
async query():
anwser = fetch($URL)
# ou
anwser = fetch($URL, {"method": "POST", "body": $PARAMS)
# [🚩] TODO: tester mode: "no-cors"
json = await anwser.json() # récupérer du JSON
text = await anwser.text() # récupérer du texte
aio.run( query() ) # [🚩] TODO: Supplier Pierre pour avoir top-level await.
```
📖 [Plus d'informations dans la documentation.](https://developer.mozilla.org/en-US/docs/Web/API/Response)
Pour construire la chaîne de paramètre :
```javascript
// [JS] Javascript
// client
const params = new URLSearchParams();
params.set($NAME, $VALUE)
fetch( `${URL}?${params.toString()}` );
// serveur
const params = new URLSearchParams($STR);
for(let key in params.keys() )
params.get(key); // retourne undefined si pas trouvé.
params.has($NAME); // retourne un booléen
params.get($NAME) ?? $DEFAULT_VALUE; // avec une valeur par défaut
```
```python
# [🐍] Python
# client
params = URLSearchParams.new()
params.set($NAME, $VALUE)
fetch( f"{URL}?{params.toString()}" )
# serveur
params = URLSearchParams.new($STR);
for key in params.keys():
params.get(key); # retourne undefined si pas trouvé.
params.has($NAME) # retourne un booléen
```
📖 [Plus d'informations dans la documentation.](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)

Loading…
Cancel
Save