parent
d14d4c2307
commit
b822840cd1
1 changed files with 100 additions and 0 deletions
@ -0,0 +1,100 @@ |
||||
## **Authorization** |
||||
|
||||
|
||||
[Zurück zur Übersicht](index.md) |
||||
|
||||
--- |
||||
|
||||
* Authorization Header |
||||
* Auth Middleware |
||||
* IP Restrictions |
||||
|
||||
### **Authorization Header** |
||||
The Auhorization header will be used to identify the client/API that connect to it. |
||||
|
||||
### **PHP** |
||||
Example to set the Header with PHP-Curl: |
||||
```php |
||||
<?php |
||||
$url = "https://api.domain.de/api/v2/PATH"; |
||||
$header = [ |
||||
"Content-Type" => "application/json", |
||||
"x-token" => "YOUR-TOKEN" // The required Auth. Header. |
||||
]; |
||||
|
||||
$curl = curl_init($url); |
||||
curl_setopt($curl, CURLOPT_URL, $url); |
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||||
$resp = curl_exec($curl); |
||||
// This Example is just to show how to set headers. |
||||
?> |
||||
``` |
||||
### **cURL** |
||||
Example to set the Header with a cURL request |
||||
|
||||
```bash |
||||
curl --location --request GET 'https://api.domain.de/api/v2/PATH' \ |
||||
--header 'x-token: YOUR-TOKEN' \ |
||||
--header 'Content-Type: application/json' |
||||
``` |
||||
### **GoLang** |
||||
Example to set the headers with GOLang. |
||||
```go |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net/http" |
||||
"io/ioutil" |
||||
) |
||||
|
||||
func main() { |
||||
|
||||
url := "https://api.domain.de/api/v2/PATH" |
||||
method := "GET" |
||||
|
||||
client := &http.Client { |
||||
} |
||||
req, err := http.NewRequest(method, url, nil) |
||||
|
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return |
||||
} |
||||
req.Header.Add("Content-Type", "application/json") |
||||
req.Header.Add("x-token", "YOUR-TOKEN") |
||||
|
||||
res, err := client.Do(req) |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return |
||||
} |
||||
defer res.Body.Close() |
||||
|
||||
body, err := ioutil.ReadAll(res.Body) |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return |
||||
} |
||||
fmt.Println(string(body)) |
||||
} |
||||
``` |
||||
|
||||
### **NodeJS** |
||||
Example to set the headers with NodeJS |
||||
|
||||
```javascript |
||||
var request = require('request'); |
||||
var options = { |
||||
'method': 'GET', |
||||
'url': 'http://api.domain.de/api/v2/PATH', |
||||
'headers': { |
||||
'x-token': 'YOUR-TOKEN', |
||||
'Content-Type': 'application/json' |
||||
} |
||||
}; |
||||
request(options, function (error, response) { |
||||
if (error) throw new Error(error); |
||||
console.log(response.body); |
||||
}); |
||||
``` |
Loading…
Reference in new issue