Skip to content

Commit 4134a18

Browse files
committed
POST should not include the target to get pushed
Signed-off-by: DL6ER <[email protected]>
1 parent 6a4a994 commit 4134a18

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

Diff for: docs/api/auth.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ As a result, the password itself is never transmitted plain-text. Eavesdropping
4141
>
4242
> Now, the annual energy output of our sun is about $1.21\cdot 10^{41}$ ergs. This is enough to power about $2.7\cdot 10^{56}$ single bit changes on our ideal computer; enough state changes to put a 187-bit counter through all its values. If we built a Dyson sphere around the sun and captured all of its energy for 32 years, without any loss, we could power a computer to count up to $2^{192}$. Of course, it wouldn’t have the energy left over to perform any useful calculations with this counter.
4343
>
44-
> But that’s just one star, and a measly one at that. A typical supernova releases something like $10^{51}$ ergs. (About a hundred times as much energy would be released in the form of neutrinos, but let them go for now.) If all of this energy could be channeled into a single orgy of computation, a `219`-bit counter could be cycled through all of its states.
44+
> But that’s just one star, and a measly one at that. A typical supernova releases something like $10^{51}$ ergs. (About a hundred times as much energy would be released in the form of neutrinos, but let them go for now.) If all of this energy could be channeled into a single orgy of computation, a 219-bit counter could be cycled through all of its states.
4545
>
4646
> These numbers have nothing to do with the technology of the devices; they are the maximums that thermodynamics will allow. And they strongly imply that **brute-force attacks against 256-bit keys will be *infeasible* until computers are built from something other than matter and occupy something other than space**.
4747
<!-- markdownlint-enable code-block-style -->

Diff for: docs/api/dns/cache.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# DNS - Cache Info
2-
31
`pihole-FTL` offers an efficient DNS cache that helps speed up your Internet experience. This DNS cache is part of the embedded `dnsmasq` server. Setting the cache size to zero disables caching. The DNS TTL value is used for determining the caching period. `pihole-FTL` clears its cache on receiving `SIGHUP`.
42

53
<!-- markdownlint-disable code-block-style -->

Diff for: docs/api/domains.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -222,33 +222,32 @@
222222

223223
**Create new entry (error on existing identical record)**
224224

225-
- `POST /api/domains/allow/exact/<domain or regex>`
226-
- `POST /api/domains/allow/regex/<domain or regex>`
227-
- `POST /api/domains/allow/exact/<domain or regex>`
228-
- `POST /api/domains/allow/regex/<domain or regex>`
225+
- `POST /api/domains/allow/exact`
226+
- `POST /api/domains/allow/regex`
227+
- `POST /api/domains/allow/exact`
228+
- `POST /api/domains/allow/regex`
229229

230230
<!-- markdownlint-disable code-block-style -->
231231
???+ example "🔒 Request"
232232

233233
=== "cURL"
234234

235235
``` bash
236-
domain="allowed2.com"
237-
curl -X POST http://pi.hole/api/domains/allow/exact/${domain} \
236+
curl -X POST http://pi.hole/api/domains/allow/exact \
238237
-d sid=$sid \
239238
-H "Content-Type: application/json" \
240-
-d '{"enabled": true, "comment": "Some text"}'
239+
-d '{"domain": "allowed2.com", "enabled": true, "comment": "Some text"}'
241240
```
242241

243242
=== "Python 3"
244243

245244
``` python
246245
import requests
247246

248-
domain = "allowed2.com"
249-
url = 'http://pi.hole:8080/api/domains/allow/exact/' + domain
247+
url = 'http://pi.hole:8080/api/domains/allow/exact'
250248
sid = '<valid session id>'
251249
data = {
250+
"item": "allowed2.com",
252251
"enabled": True,
253252
"comment": "Some text",
254253
"sid": sid
@@ -261,6 +260,9 @@
261260

262261
**Optional parameters**
263262

263+
??? info "Domain/regex (`"item": string`)"
264+
Domain or regex to be added. When this is a regular expression, ensure it is properly JSON-escaped.
265+
264266
??? info "Enabled (`"enabled": boolean`)"
265267
Whether this item should enabled or not.
266268

@@ -309,7 +311,7 @@
309311
}
310312
```
311313

312-
!!! hint "Hint: Use `PUT` instead of `POST`"
314+
!!! hint "Hint: Use `PUT` instead of `POST` to avoid unique constraints"
313315
When using `PUT` instead of `POST`, duplicate domains are silently replaced without issuing an error.
314316
<!-- markdownlint-enable code-block-style -->
315317

Diff for: docs/api/index.md

+35-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# API Reference
22

3-
The Pi-hole API is organized around [REST](http://en.wikipedia.org/wiki/Representational_State_Transfer). Our API has predictable resource-oriented URLs, accepts and returns reliable UTF-8 [JSON-encoded](http://www.json.org/) data for all API responses, and uses standard HTTP response codes and verbs.
3+
The Pi-hole API is organized around [REST](http://en.wikipedia.org/wiki/Representational_State_Transfer). Our API has predictable resource-oriented URLs, accepts and returns reliable UTF-8 [JavaScript Object Notation (JSON)-encoded](http://www.json.org/) data for all API responses, and uses standard HTTP response codes and verbs.
44

55
Most (but not all) endpoints require authentication. API endpoints requiring authentication will fail with code `401 Unauthorized` if no key is supplied.
66

@@ -110,45 +110,61 @@ In contrast, errors have a uniform style to ease their programatic treatment:
110110

111111
## HTTP methods used by this API
112112

113-
Each HTTP request consists of a method that indicates the action to be performed on the identified resource. The relevant standards are [RFC 2616, Scn. 9](https://tools.ietf.org/html/rfc2616#section-9) (`GET/POST/PUT/DELETE`).
113+
Each HTTP request consists of a method that indicates the action to be performed on the identified resource. The relevant standards is [RFC 2616](https://tools.ietf.org/html/rfc2616). Though, RFC 2616 has been very clear in differentiating between the methods, complex wordings are a source of confusion for many users.
114114

115115
Pi-hole's API uses the methods like this:
116116

117117
Method | Description
118118
---------|------------
119119
`GET` | **Read** from resource. The resource may not exist.
120120
`POST` | **Create** resources
121-
`PUT` | **Create or Replace** the resource. This method commonly used to *update* entries.
121+
`PUT` | **Create or Replace** a resource. This method is commonly used to *update* entries.
122122
`DELETE` | **Delete** existing resource
123123

124-
### `GET`
124+
<!-- markdownlint-disable code-block-style -->
125+
??? info "Summarized details from [RFC 2616, Scn. 9](https://tools.ietf.org/html/rfc2616#section-9) (`GET/POST/PUT/DELETE`)"
126+
### `GET`
127+
128+
The `GET` method means retrieve whatever information (in the form of an entity) that is identified by the URI.
129+
130+
As `GET` requests do not change the state of the resource, these are said to be **safe methods**. Additionally, `GET` requests are **idempotent**, which means that making multiple identical requests must produce the same result every time until another method (`POST` or `PUT`) has changed the state of the resource on the server.
125131

126-
The `GET` method means retrieve whatever information (in the form of an entity) that is identified by the Request-URI.
132+
For any given HTTP `GET`, if the resource is found on the server, then the API returns HTTP response code `200 (OK)` – along with the response body.
127133

128-
### `POST`
134+
In case a resource is NOT found on server, then the API returns HTTP response code `404 (Not found)`. Similarly, if it is determined that `GET` request itself is not correctly formed then API will return HTTP response code `400 (Bad request)`.
129135

130-
The `POST` method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. `POST` is designed to allow a uniform method to cover the following functions:
136+
### `POST`
131137

132-
- Annotation of existing resources;
133-
- Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;
134-
- Providing a block of data, such as the result of submitting a form, to a data-handling process;
135-
- Extending a database through an append operation.
138+
Use `POST` APIs to **create new subordinate records**, e.g., a file is subordinate to a directory containing it or a row is subordinate to a database table. W`POST` methods are used to create a new resource into the collection of resources.
136139

137-
If a resource has been created on the origin server, the response will be `201 (Created)`.
140+
If a resource has been created on the origin server, the response will be `201 (Created)`.
141+
Not all action performed using the `POST` method will result in a resource that can be identified by a URI. In such a case, either `200 (OK)` or `204 (No Content)` is the appropriate response status, depending on whether or not the response includes an entity that describes the result.
138142

139-
Not all action performed using the `POST` method will result in a resource that can be identified by a URI. In such a case, either `200 (OK)` or `204 (No Content)` is the appropriate response status, depending on whether or not the response includes an entity that describes the result.
143+
Note that `POST` is **neither safe nor idempotent**, and invoking two identical `POST` requests typically results in an error.
140144

141-
### `PUT`
145+
### `PUT`
142146

143-
The `PUT` method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity will be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.
147+
Use `PUT` primarily to **update existing records** (if the resource does not exist, the API will typically create a new record for it). If a new record has been added at the given URI, or an existing resource is modified, either the `200 (OK)` or `204 (No Content)` response codes are sent to indicate successful completion of the request.
144148

145-
The origin server will inform the user agent via a `200 (OK)` response if processing the entry was successful.
149+
### `DELETE`
146150

147-
### `DELETE`
151+
As the name applies, `DELETE` APIs are used to **delete records** (identified by the Request-URI).
148152

149-
The `DELETE` method requests that the origin server delete the resource identified by the Request-URI. The API will not indicate success unless, at the time the response is given, it intends to delete the resource or move it to an inaccessible location.
153+
A successful response will be `200 (OK)` if the response includes an entity describing the status, `202 (Accepted)` if the action has not yet been enacted, or `204 (No Content)` if the action has been enacted but the response does not include an entity.
150154

151-
A successful response will be `200 (OK)` if the response includes an entity describing the status, `202 (Accepted)` if the action has not yet been enacted, or `204 (No Content)` if the action has been enacted but the response does not include an entity.
155+
`DELETE` operations are **idempotent**. If you `DELETE` a resource, it’s removed from the collection of resources. Repeatedly calling `DELETE` on that resource will not change the outcome – however, calling `DELETE` on a resource a second time *may* return a 404 (NOT FOUND) since it was already removed.
156+
157+
???+ info "Example"
158+
Let’s list down few URIs and their purpose to get better understanding when to use which method:
159+
160+
Method + URI | Interpretation
161+
---------------------|--------------------
162+
`GET /api/groups` | Get all groups
163+
`POST /api/groups` | Create a new group
164+
`GET /api/groups/abc` | Get the group `abc`
165+
`PUT /api/groups/abc` | Update the group `abc`
166+
`DELETE /api/groups/abc` | Delete group `abc`
167+
<!-- markdownlint-enable code-block-style -->
152168

153169
## Error handling
154170

0 commit comments

Comments
 (0)