Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mauserzjeh committed May 8, 2022
1 parent 4d27a30 commit d4f793c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,32 +121,58 @@ pingo.SetOptions(client,


## Request object

#### Empty request
```go
// Create a request object with no body
req := NewEmptysRequest()
```

#### Request from raw bytes
```go
// Create a request with a body from a byte slice
req := NewRequest([]byte(`this is the request body`))
```

#### JSON request from custom struct
```go
type MyData struct {
Field1 string `json:"field1"`
Field2 int64 `json:"field2"`
}

data := MyData{
Field1: "value",
Field2: 1,
}

// Create a json request with a body from any data.
// Data can be any type of variable or a struct with proper `json:"fieldname"` tags.
// An error is returned if the marshaling of the data resulted in an error.
// "Content-Type: application/json" header is added to the request.
req, err := NewJsonRequest(data)
```

#### Form request from custom struct
```go
type MyData struct {
Field1 string `form:"field1"`
Field2 int64 `form:"field2"`
}

data := MyData{
Field1: "value",
Field2: 1,
}

// Create a form request with a body from any data.
// Data can be any type of map or a struct with proper `form:"fieldname"` tags.
// An error is returned if the marshaling of the data resulted in an error.
// "Content-Type: application/x-www-form-urlencoded" header is added to the request
req, err := NewFormRequest(data)
```

#### Request fields
```go
// After creating the request, additional options can be set.
req.Method = pingo.POST // GET by default
Expand All @@ -157,11 +183,14 @@ req.QueryParams.Set("Foo", "Bar") // If necessary addtional query parameters can
```

## Response object

#### Plain response
```go
// Create a response object
res := NewResponse()
```

#### JSON response with custom struct
```go
type MyData struct {
Field1 string `json:"field1"`
Expand All @@ -174,6 +203,7 @@ type MyData struct {
res := NewJsonResponse(&MyData{})
```

#### Custom response handling via handler function
```go
// Response struct for good response
type Good struct {
Expand Down

0 comments on commit d4f793c

Please sign in to comment.