Skip to content

Commit

Permalink
Add C example
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Nov 2, 2023
1 parent 2953f24 commit b89107e
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions docs/api/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ To get a session ID, you will have to send a `POST` request to the `/api/auth` e
<!-- markdownlint-disable code-block-style -->
???+ example "Authentication with password"

=== "cURL"
=== "bash / cURL"

``` bash
curl -k -X POST "https://pi.hole/admin/api/auth" --data '{"password":"your-password"}'
Expand Down Expand Up @@ -54,6 +54,42 @@ To get a session ID, you will have to send a `POST` request to the `/api/auth` e
console.log(error);
});
```

=== "C"

``` c
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

int main(void)
{
CURL *curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://pi.hole/admin/api/auth");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"password\":\"your-password\"}");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

res = curl_easy_perform(curl);

curl_easy_cleanup(curl);
}

curl_global_cleanup();

return 0;
}
```

**Parameters**
Expand Down Expand Up @@ -108,7 +144,7 @@ Note that when using cookie-based authentication, you will also need to send a `

???+ example "Authentication with SID"

=== "cURL"
=== "bash / cURL"

``` bash
# Example: Authentication with SID in the request URI
Expand Down Expand Up @@ -184,7 +220,7 @@ If you have 2FA enabled for your Pi-hole, you will need to provide a TOTP token

???+ example "Authentication with 2FA"

=== "cURL"
=== "bash / cURL"

``` bash
curl -k -X POST "https://pi.hole/admin/api/auth" --data '{"password":"your-password", "totp":"123456"}'
Expand Down Expand Up @@ -289,7 +325,7 @@ To end your session before the SID expires, you can send a `DELETE` request to t

???+ example "Logout"

=== "cURL"
=== "bash / cURL"

``` bash
# Example: Logout with SID in the request URI
Expand Down

0 comments on commit b89107e

Please sign in to comment.