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

jeffotoni / quick / 221

06 Mar 2025 07:12PM UTC coverage: 49.324% (-1.2%) from 50.54%
221

push

circleci

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

0 of 89 new or added lines in 3 files covered. (0.0%)

1825 of 3700 relevant lines covered (49.32%)

2697.54 hits per line

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

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

3
import (
4
        "context"
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 handles PUT requests to update user data on an external API.
15
// - WithTimeout: Sets the HTTP client timeout to 10 seconds.
16
// - WithContext: Attaches a background context to the client which is not cancellable.
17
// - WithHeaders: Adds custom headers for content type and authorization.
18
// - WithDisableKeepAlives: Enables or disables HTTP keep-alives (false = keep-alives enabled).
19

NEW
20
func main() {
×
NEW
21
        q := quick.New()
×
NEW
22

×
NEW
23
        // Define a PUT endpoint to update user data.
×
NEW
24
        q.Put("/api/users/2", func(c *quick.Ctx) error {
×
NEW
25
                // Read the request body from the client
×
NEW
26
                requestBody, err := io.ReadAll(c.Request.Body)
×
NEW
27
                if err != nil {
×
NEW
28
                        log.Println("Read Error:", err)
×
NEW
29
                        return c.Status(500).SendString("Failed to read request body")
×
NEW
30
                }
×
31

32
                // Create an HTTP client with specific configurations.
NEW
33
                cClient := client.New(
×
NEW
34
                        // Set the timeout for the HTTP client to 10 seconds.
×
NEW
35
                        client.WithTimeout(10*time.Second),
×
NEW
36
                        // Add custom headers, including content type and authorization token.
×
NEW
37
                        client.WithHeaders(map[string]string{
×
NEW
38
                                "Content-Type":  "application/json",
×
NEW
39
                                "Authorization": "Bearer EXAMPLE_TOKEN",
×
NEW
40
                        }),
×
NEW
41
                        // Use a background context for the HTTP client. This context cannot be cancelled
×
NEW
42
                        // and does not carry any deadline. It is suitable for operations that run
×
NEW
43
                        // indefinitely or until the application is shut down.
×
NEW
44
                        client.WithContext(context.Background()),
×
NEW
45
                )
×
NEW
46

×
NEW
47
                // Perform a PUT request to the external API with the data received from the client.
×
NEW
48
                resp, err := cClient.Put("https://reqres.in/api/users/2", requestBody)
×
NEW
49
                if err != nil {
×
NEW
50
                        log.Println("PUT Error:", err)
×
NEW
51
                        return c.Status(500).SendString("Failed to connect to external API")
×
NEW
52
                }
×
53

54
                // Log and return the response body from the external API.
NEW
55
                log.Println("PUT Response:", string(resp.Body))
×
NEW
56
                return c.Status(resp.StatusCode).Send(resp.Body)
×
57
        })
58

59
        // Start listening on port 3000 for incoming HTTP requests.
NEW
60
        q.Listen(":3000")
×
61
}
62

63
// curl -X PUT https://reqres.in/api/users/2 \
64
//      -H "Content-Type: application/json" \
65
//      -d '{"name": "Morpheus", "job": "zion resident"}'
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