• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jeffotoni / quick / 219

06 Mar 2025 06:32PM UTC coverage: 50.442% (+1.3%) from 49.125%
219

push

circleci

jeffotoni
Merge remote-tracking branch 'origin/example/http'

0 of 47 new or added lines in 2 files covered. (0.0%)

1824 of 3616 relevant lines covered (50.44%)

2760.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/example/quick.http.client/quick.post.custom/main.go
1
package main
2

3
import (
4
        "encoding/json"
5
        "io"
6
        "log"
7
        "time"
8

9
        "github.com/jeffotoni/quick"
10
        "github.com/jeffotoni/quick/http/client"
11
)
12

13
// Example HTTP request using Quick and an external API (reqres.in).
14
// This server listens on port 3000 and forwards POST requests to an external API.
15
//
16
// - WithTimeout: Sets the HTTP client timeout to 5 seconds.
17
// - WithDisableKeepAlives: Enables or disables HTTP keep-alives (false = keep-alives enabled).
18
// - WithHeaders: Adds custom headers (e.g., Content-Type: application/json).
NEW
19
func main() {
×
NEW
20
        // Initialize the Quick framework.
×
NEW
21
        q := quick.New()
×
NEW
22

×
NEW
23
        // Define a POST endpoint to process incoming requests.
×
NEW
24
        q.Post("/api/users", func(c *quick.Ctx) error {
×
NEW
25
                // Read the request body
×
NEW
26
                body, err := io.ReadAll(c.Request.Body)
×
NEW
27
                if err != nil {
×
NEW
28
                        return c.Status(400).SendString("Error reading request body: " + err.Error())
×
NEW
29
                }
×
30

31
                // Check if the request body is empty
NEW
32
                if len(body) == 0 {
×
NEW
33
                        return c.Status(400).SendString("Error: Request body is empty")
×
NEW
34
                }
×
35

36
                // Validate that the request body is valid JSON
NEW
37
                var jsonData map[string]interface{}
×
NEW
38
                if err := json.Unmarshal(body, &jsonData); err != nil {
×
NEW
39
                        return c.Status(400).SendString("Error: Invalid JSON")
×
NEW
40
                }
×
41

42
                // Create a modular HTTP client with customizable options.
NEW
43
                cClient := client.New(
×
NEW
44
                        // Sets the HTTP timeout to 5 seconds.
×
NEW
45
                        client.WithTimeout(5*time.Second),
×
NEW
46

×
NEW
47
                        // Enables or disables HTTP Keep-Alive connections (false = keep-alives enabled).
×
NEW
48
                        client.WithDisableKeepAlives(false),
×
NEW
49

×
NEW
50
                        // Adds custom headers to the request, including Content-Type and Authorization.
×
NEW
51
                        client.WithHeaders(map[string]string{
×
NEW
52
                                "Content-Type":  "application/json",
×
NEW
53
                                "Authorization": "Bearer EXAMPLE_TOKEN",
×
NEW
54
                        }),
×
NEW
55
                )
×
NEW
56

×
NEW
57
                // Forward the request to the external API
×
NEW
58
                resp, err := cClient.Post("https://reqres.in/api/users", json.RawMessage(body))
×
NEW
59
                if err != nil {
×
NEW
60
                        log.Println("Error making request to external API:", err)
×
NEW
61
                        return c.Status(500).SendString("Error connecting to external API")
×
NEW
62
                }
×
63

64
                // Log response from external API for debugging
NEW
65
                log.Println("External API response:", string(resp.Body))
×
NEW
66

×
NEW
67
                // Return the response from the external API to the client
×
NEW
68
                return c.Status(resp.StatusCode).Send(resp.Body)
×
69
        })
70

71
        // Start the server on port 3000
NEW
72
        q.Listen(":3000")
×
73
}
74

75
// curl -X POST http://localhost:3000/api/users \
76
//      -H "Content-Type: application/json" \
77
//      -H "Authorization: Bearer EXAMPLE_TOKEN" \
78
//      -d '{"name": "John Doe", "job": "Software Engineer"}'
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc