How to access the Azure App Service Log from a code with go lang

Jyoti Mallick 40 Reputation points
2025-01-07T11:18:28.9233333+00:00

Hi Community,

I want to access the Azure app service's log from an internal code using Golang. All I have is an access key. Please put some insights here.

I have my app service running on Azure,https://<prodserver>.scm.azurewebsites.net/ and the log files are maintained under /wwwroot/logs/.

If I want to access it from the browser, I am able to access through https://<prodserver>.scm.azurewebsites.net/wwwroot/logs/

But I want to access it from the code with GoLang. I have an access key to access the request. Can someone please help me to achieve this? When I am using Access Key in the code, I am getting https status code 403.

Can some one help me how to access these log files without any hindrance??

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,961 questions
{count} votes

Accepted answer
  1. Shree Hima Bindu Maganti 4,925 Reputation points Microsoft External Staff Moderator
    2025-01-20T06:20:59.24+00:00

    Hi @Jyoti Mallick
    Sorry for the delay Response.
    Based on your updated Go code and error details, the 401 Unauthorized error indicates that the access key being used for authentication is invalid or is not being applied correctly.

    • Download the Publishing Profile from your App Service in the Azure Portal.
    • Extract the username and password from the .PublishSettings file.
    • Encode the credentials as username:password in Base64 auth := base64.StdEncoding.EncodeToString([]byte("<username>:<password>"))
    • Use the Authorization header with Basic authentication req.Header.Set("Authorization", "Basic "+auth)
    • Use the correct Kudu API URL https://<app-service-name>.scm.azurewebsites.net/api/vfs/site/wwwroot/logs/
    • Updated Code .
        package main
        import (
        	"encoding/base64"
        	"fmt"
        	"io/ioutil"
        	"log"
        	"net/http"
        )
        func main() {
        	username := "<username-from-publishing-profile>"
        	password := "<password-from-publishing-profile>"
        	url := "https://<app-service-name>.scm.azurewebsites.net/api/vfs/site/wwwroot/logs/"
        	// Prepare Basic authentication header
        	auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
        	// Create HTTP GET request
        	req, err := http.NewRequest("GET", url, nil)
        	if err != nil {
        		log.Fatalf("Error creating request: %v", err)
        	}
        	req.Header.Set("Authorization", "Basic "+auth)
        	// Execute the request
        	client := &http.Client{}
        	resp, err := client.Do(req)
        	if err != nil {
        		log.Fatalf("Request failed: %v", err)
        	}
        	defer resp.Body.Close()
        	// Handle response
        	if resp.StatusCode != http.StatusOK {
        		log.Fatalf("Error: %s", resp.Status)
        	}
        	body, err := ioutil.ReadAll(resp.Body)
        	if err != nil {
        		log.Fatalf("Error reading response body: %v", err)
        	}
        	// Print log files
        	fmt.Println(string(body))
        }
      
    • Use curl to validate curl -u <username>:<password> https://<app-service-name>.scm.azurewebsites.net/api/vfs/site/wwwroot/logs/
      If the answer is helpful, please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.