Observing 401 http status code using the FTP credentials from a go lang code while accessing the Azure App Service long using SCM kudu service

Jyoti Mallick 40 Reputation points
2025-01-16T06:41:39.2933333+00:00

Problem Statement: I have an Azure app service, and its log is generated under the https://<prod_server>.scm.azurewebsites.net/wwwroot/logs/. I am able to access it from the browser as my default credential is already logged in with. I am trying to achieve the same using Golang code running from my code editor (i.e. Visual Studio Code).

I am using the credentials: (please refer the screenshot below)

User's image

I am able to access it from the browser:

User's image

Sample GoLong Code:

package main
import (
	"encoding/base64"
	"fmt"
	"log"
	"net/http"
)
func main() {
	// Replace with your correct SCM Access Key
	accessKey := "<ftp_password>" // Use the publishing password from the output
	// Replace with your Azure App Service SCM URL
	url := fmt.Sprintf("https://<prod_server>.scm.azurewebsites.net/api/vfs/site/wwwroot/logs/")
	// Prepare Basic authentication header
	// Ensure to escape the backslash correctly
	username := "<user_username>" // Escaped backslash in username
	auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + accessKey))
	// 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)
	}
	// Print log files or handle as needed
	fmt.Println("Logs retrieved successfully")
}

Note: My user name is like username\$username (would that be a problem? just thought to mention).

Why am I observing the below error?

Now if I try the same user name and password in FTP tools like WinScp, it works like a charm. But the above code is giving me the following error:

2025/01/16 12:03:25 Error: 401 Unauthorized
exit status 1
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,933 questions
{count} votes

Accepted answer
  1. Shree Hima Bindu Maganti 4,775 Reputation points Microsoft External Staff Moderator
    2025-01-23T16:34:31.1166667+00:00

    Hi @Jyoti Mallick
    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer.
    The user name was the culprit here; you don't need to send the username\\$username rather it should be only $username`

    package main
    import (
    	"encoding/base64"
    	"fmt"
    	"io/ioutil"
    	"log"
    	"net/http"
    )
    func main() {
    	// Replace with your correct SCM Access Key
    	accessKey := "<Password>" // Use the publishing password from the output
    	// Replace with your Azure App Service SCM URL
    	url := fmt.Sprintf("https://<URL>.scm.azurewebsites.net/api/vfs/site/wwwroot/logs")
    	// Prepare Basic authentication header
    	// Ensure to escape the backslash correctly
    	username := "$<username>" // Escaped backslash in username
    	auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + accessKey))
    	// 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 or handle as needed
    	fmt.Println("Logs retrieved successfully")
    	fmt.Println(string(body))
    }
    

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jyoti Mallick 40 Reputation points
    2025-01-23T12:59:59.41+00:00

    The user name was the culprit here; you dont need to send the username\\$username rather it should be only $username`

    Please follow the code,

    package main
    import (
    	"encoding/base64"
    	"fmt"
    	"io/ioutil"
    	"log"
    	"net/http"
    )
    func main() {
    	// Replace with your correct SCM Access Key
    	accessKey := "<Password>" // Use the publishing password from the output
    	// Replace with your Azure App Service SCM URL
    	url := fmt.Sprintf("https://<URL>.scm.azurewebsites.net/api/vfs/site/wwwroot/logs")
    	// Prepare Basic authentication header
    	// Ensure to escape the backslash correctly
    	username := "$<username>" // Escaped backslash in username
    	auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + accessKey))
    	// 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 or handle as needed
    	fmt.Println("Logs retrieved successfully")
    	fmt.Println(string(body))
    }
    
    
    0 comments No comments

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.