Skip to content

Commit 08b5776

Browse files
committed
doc: mention the HTTP status code used for streaming & fix client code
1 parent 2f538c2 commit 08b5776

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

doc/12-icinga2-api.md

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ was malformed.
154154
A status in the range of 500 generally means that there was a server-side problem
155155
and Icinga 2 is unable to process your request.
156156

157+
Starting with Icinga 2 v2.16.0 we introduced HTTP chunked transfer encoding for some of the API endpoints.
158+
As a consequence, the `/v1/actions/` and `/v1/objects/` endpoints will always return a `202 Accepted` status code as a
159+
general HTTP status, but the status of each individual operation remains the same as before, and will continue to report
160+
`2xx`, `4xx` or `5xx` status codes. For example, you may see such a response when trying to delete multiple objects.
161+
162+
```json
163+
{
164+
"results": [
165+
{
166+
"code": 200,
167+
"status": "Object was deleted."
168+
},
169+
{
170+
"code": 500,
171+
"status": "Object could not be deleted.",
172+
}
173+
]
174+
}
175+
```
176+
177+
In such cases, you should always check the individual result entries for their status code and not rely solely on the
178+
overall HTTP status code. There are also a number of other endpoints which use chunked transfer encoding, but have no
179+
behavioral difference in terms of the overall HTTP status code and will continue to return `200` for successful requests.
180+
157181
### Security <a id="icinga2-api-security"></a>
158182

159183
* HTTPS only.
@@ -2742,7 +2766,7 @@ r = requests.post(request_url,
27422766
print "Request URL: " + str(r.url)
27432767
print "Status code: " + str(r.status_code)
27442768
2745-
if (r.status_code == 200):
2769+
if (r.status_code == 202):
27462770
print "Result: " + json.dumps(r.json())
27472771
else:
27482772
print r.text
@@ -2791,7 +2815,7 @@ rescue => e
27912815
end
27922816
27932817
puts "Status: " + response.code.to_s
2794-
if response.code == 200
2818+
if response.code == 202
27952819
puts "Result: " + (JSON.pretty_generate JSON.parse(response.body))
27962820
else
27972821
puts "Error: " + response
@@ -2846,7 +2870,7 @@ $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
28462870
curl_close($ch);
28472871
print "Status: " . $code . "\n";
28482872
2849-
if ($code == 200) {
2873+
if ($code == 202) {
28502874
$response = json_decode($response, true);
28512875
print_r($response);
28522876
}
@@ -2898,7 +2922,7 @@ $client->POST("/v1/objects/services", $data);
28982922
my $status = $client->responseCode();
28992923
print "Status: " . $status . "\n";
29002924
my $response = $client->responseContent();
2901-
if ($status == 200) {
2925+
if ($status == 202) {
29022926
print "Result: " . Dumper(decode_json($response)) . "\n";
29032927
} else {
29042928
print "Error: " . $response . "\n";
@@ -2921,14 +2945,13 @@ import (
29212945
"bytes"
29222946
"crypto/tls"
29232947
"log"
2924-
"io/ioutil"
29252948
"net/http"
29262949
)
29272950
29282951
func main() {
2929-
var urlBase= "https://localhost:5665"
2930-
var apiUser= "root"
2931-
var apiPass= "icinga"
2952+
var urlBase = "https://localhost:5665"
2953+
var apiUser = "root"
2954+
var apiPass = "icinga"
29322955
29332956
urlEndpoint := urlBase + "/v1/objects/services"
29342957
@@ -2943,7 +2966,7 @@ func main() {
29432966
"filter": "match(\"ping*\", service.name)"
29442967
}`)
29452968
2946-
req, err := http.NewRequest("POST", urlEndpoint, bytes.NewBuffer(requestBody))
2969+
req, err := http.NewRequest("POST", urlEndpoint, bytes.NewReader(requestBody))
29472970
req.Header.Set("Accept", "application/json")
29482971
req.Header.Set("X-HTTP-Method-Override", "GET")
29492972
@@ -2958,22 +2981,24 @@ func main() {
29582981
29592982
log.Print("Response status:", resp.Status)
29602983
2961-
bodyBytes, _ := ioutil.ReadAll(resp.Body)
2962-
bodyString := string(bodyBytes)
2984+
buf := new(bytes.Buffer)
2985+
if _, err := buf.ReadFrom(resp.Body); err != nil {
2986+
log.Fatal("Read error:", err)
2987+
return
2988+
}
29632989
2964-
if resp.StatusCode == http.StatusOK {
2965-
log.Print("Result: " + bodyString)
2990+
if resp.StatusCode == http.StatusAccepted {
2991+
log.Print("Result: " + buf.String())
29662992
} else {
2967-
log.Fatal(bodyString)
2993+
log.Fatal(buf.String())
29682994
}
29692995
}
29702996
```
29712997

29722998
Build the binary:
29732999

29743000
```bash
2975-
go build icinga.go
2976-
./icinga
3001+
go run ./icinga.go
29773002
```
29783003

29793004
#### Example API Client in Powershell <a id="icinga2-api-clients-programmatic-examples-powershell"></a>

0 commit comments

Comments
 (0)