Hone logo
Hone
Problems

Building a Simple Echo Handler in Go

This challenge focuses on creating a fundamental building block of Go web applications: an http.HandlerFunc. An http.HandlerFunc is a function that satisfies the http.Handler interface, allowing it to handle incoming HTTP requests. This is a core concept for building web servers in Go, and mastering it is essential for more complex applications.

Problem Description

You are tasked with creating an http.HandlerFunc named echoHandler that receives an http.ResponseWriter and an *http.Request as input. This handler should read the "message" query parameter from the request. If the "message" parameter is present, it should write the value of the "message" parameter back to the client as the response body. If the "message" parameter is not present, it should write a default message "No message provided" to the client. The response should always be plain text.

Key Requirements:

  • Implement the echoHandler function as an http.HandlerFunc.
  • Extract the "message" query parameter from the http.Request.
  • Handle the case where the "message" parameter is missing.
  • Write the appropriate response to the http.ResponseWriter.
  • Ensure the response is plain text.

Expected Behavior:

  • If the request includes a query parameter message=some_value, the response body should be "some_value".
  • If the request does not include the message query parameter, the response body should be "No message provided".
  • The response status code should be 200 OK in both cases.

Edge Cases to Consider:

  • Empty "message" value (e.g., message=). Should still be treated as a valid message.
  • Invalid URL encoding of the "message" parameter. The standard library's url.Values handles this automatically, so you don't need to implement your own URL decoding.

Examples

Example 1:

Input: GET /?message=Hello%20World
Output: Hello World
Explanation: The request includes the message parameter with the value "Hello World" (URL encoded). The handler extracts this value and writes it to the response.

Example 2:

Input: GET /
Output: No message provided
Explanation: The request does not include the message parameter. The handler writes the default message.

Example 3:

Input: GET /?message=
Output: (empty string)
Explanation: The request includes the message parameter, but its value is empty. The handler writes the empty string to the response.

Constraints

  • The function must be an http.HandlerFunc.
  • The response must be plain text (Content-Type: text/plain).
  • The response status code must be 200 OK.
  • The function should handle URL-encoded query parameters correctly.
  • The function should not panic under any valid input conditions.

Notes

  • Use the http.ResponseWriter to write the response body and set the status code.
  • Use the http.Request.URL.Query() method to access the query parameters.
  • Remember to import the net/http package.
  • Consider using fmt.Fprintf to write to the http.ResponseWriter.
  • Focus on the core logic of handling the request and generating the response. Error handling beyond the specified requirements is not necessary for this challenge.
Loading editor...
go