curl -v -X POST http:\/\/localhost:8080\/example -H \"Authorization: Basic YWJjOjEyMw==\"<\/code><\/pre>\n\n\n\nThis request will get the status code as 200. Also it will correctly print the username and password in the logs as well.<\/p>\n\n\n\n
Username: abc\nPassword: 123<\/code><\/pre>\n\n\n\nNow send the malformed Base64<\/strong> encoded value. <\/p>\n\n\n\ncurl -v -X POST http:\/\/localhost:8080\/example -H \"Authorization: Basic YWJjOjEy\"<\/code><\/pre>\n\n\n\nIt will get the status code as 401. It will also print below in the logs<\/p>\n\n\n\n
Error parsing basic auth<\/code><\/pre>\n\n\n\n<\/span>HTTP Client Basic Auth Example<\/strong><\/span><\/h1>\n\n\n\nnet\/http<\/strong> package of golang also provides a method which is defined on the *http.Request<\/strong> struct which can be used to set the basic auth header. Below is the signature of the method<\/p>\n\n\n\nfunc (r *Request) SetBasicAuth(username, password string)<\/code><\/pre>\n\n\n\nWhat this method does it takes in username<\/strong> and password<\/strong> and sets Authorization header with base64 encoded value of username and password joined by a single colon(:).<\/p>\n\n\n\nLet’s see a program for this<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"net\/http\"\n\t\"time\"\n)\n\nvar (\n\tusername = \"abc\"\n\tpassword = \"123\"\n)\n\nfunc main() {\n\tcall(\"https:\/\/localhost:8080\/example\", \"POST\")\n}\n\nfunc call(url, method string) error {\n\tclient := &http.Client{\n\t\tTimeout: time.Second * 10,\n\t}\n\treq, err := http.NewRequest(method, url, nil)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Got error %s\", err.Error())\n\t}\n\treq.SetBasicAuth(username, password)\n\tresponse, err := client.Do(req)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Got error %s\", err.Error())\n\t}\n\tdefer response.Body.Close()\n\treturn nil\n}<\/code><\/pre>\n\n\n\nIn the above program we are making a call to the server which we had set up earlier. See how we set up the basic auth in the outgoing request<\/p>\n\n\n\n
req.SetBasicAuth(username, password)<\/code><\/pre>\n\n\n\nIf you will run the above program you will get a 200 from the server which we had set up above.<\/p>\n\n\n\n
Note:<\/strong> We are printing username<\/strong> and password<\/strong> above just for illustration purposes. In real program they need to be kept encrypted in some secure manner. Also we are sending a http request below and not https for basic auth. Again this was just for illustrated only and both client and server lie on the same machine. This is not recommended and basic auth should be used only with HTTPS<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewHTTP Server Basic Auth ExampleHTTP Client Basic Auth Example Overview Basic auth is the simplest form of providing access controls for resources on web server. Basic Access Authentication is…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-4746","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
HTTP Client\/Server with Basic Auth in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n