<\/span><\/h1>\n\n\n\nBelow is the program for the same. This is how we created the body in the program<\/p>\n\n\n\n
data := url.Values{}\ndata.Set(\"name\", \"John\")\ndata.Set(\"age\", \"18\")\ndata.Add(\"hobbies\", \"sports\")\ndata.Add(\"hobbies\", \"music\")<\/code><\/pre>\n\n\n\nDifference between Add and Set<\/p>\n\n\n\n
Add<\/strong> will append an extra value to the key in url.Values <\/strong>map<\/li><\/ul>\n\n\n\nSet<\/strong> will replace the existing key in url.Values<\/strong> map<\/li><\/ul>\n\n\n\nAfter that, we need to encode the data. Then encoded data is passed to the http.NewRequest<\/strong> function. Also don’t forget to set the content-type header to application\/x-www-form-urlencoded<\/strong><\/p>\n\n\n\nencodedData := data.Encode()\nhttp.NewRequest(method, urlPath, strings.NewReader(encodedData))<\/code><\/pre>\n\n\n\nThe full program for the same.<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"net\/http\"\n\t\"net\/url\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n)\n\nfunc main() {\n\tcall(\"http:\/\/localhost:8080\/employee\", \"POST\")\n}\n\nfunc call(urlPath, method string) error {\n\tclient := &http.Client{\n\t\tTimeout: time.Second * 10,\n\t}\n\n\tdata := url.Values{}\n\tdata.Set(\"name\", \"John\")\n\tdata.Set(\"age\", \"18\")\n\tdata.Add(\"hobbies\", \"sports\")\n\tdata.Add(\"hobbies\", \"music\")\n\tencodedData := data.Encode()\n\tfmt.Println(encodedData)\n\treq, err := http.NewRequest(method, urlPath, strings.NewReader(encodedData))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Got error %s\", err.Error())\n\t}\n\treq.Header.Add(\"Content-Type\", \"application\/x-www-form-urlencoded\")\n\treq.Header.Add(\"Content-Length\", strconv.Itoa(len(data.Encode())))\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\nIf you run a server that listens on :8080 port similar to the below link, then you will see that the server will be able to correctly print the body of the incoming request<\/p>\n\n\n\n
https:\/\/golangbyexamples.com\/url-encoded-body-golang<\/a>\/<\/p>\n\n\n\nAlso the output of<\/p>\n\n\n\n
fmt.Println(encodedData)<\/code><\/pre>\n\n\n\nwill be<\/p>\n\n\n\n
age=18&hobbies=sports&hobbies=music&name=John<\/code><\/pre>\n\n\n\nwhich is in the same format that we had discussed above.<\/p>\n","protected":false},"excerpt":{"rendered":"
Table of Contents OverviewExample Overview The applcation\/x-www-form-urlencoded content-type request body is like a giant query string. Similar to the query string in a URI it is a key-value pair having the below…<\/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-4851","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
HTTP client or Send x-www-form-urlencoded request body 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