Go Code Example
Here’s a sample Go code for integrating with the FraudGuard.io API: and associated explanation.
This Go code performs the following steps:
- Sets the FraudGuard.io API credentials (username and password).
- Defines the IP address to query.
- Constructs the API endpoint URL.
- Creates an HTTP client.
- Creates an HTTP request with the GET method for the API endpoint.
- Sets the basic authentication header using Base64 encoding.
- Sends the HTTP request and retrieves the response.
- Reads the response body and prints it.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"encoding/base64"
)
func main() {
// FraudGuard.io API credentials
username := "username"
password := "password"
// IP address to query
ipAddress := "8.8.8.8"
// API endpoint
apiUrl := "https://api.fraudguard.io/ip/" + ipAddress
// Create HTTP client
client := &http.Client{}
// Create request
req, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Set basic authentication header
authString := username + ":" + password
authHeaderValue := "Basic " + base64.StdEncoding.EncodeToString([]byte(authString))
req.Header.Add("Authorization", authHeaderValue)
// Send request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
// Read response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return
}
// Print response
fmt.Println(string(body))
}
Make sure to replace ‘username’ and ‘password’ with your actual API credentials.