2017-01-01 21:39:05 +03:00
|
|
|
# HTTP Client
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
This plugin is a http client for micro.
|
2017-01-01 21:39:05 +03:00
|
|
|
|
2018-11-23 17:25:46 +03:00
|
|
|
## Overview
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
The http client wraps `net/http` to provide a robust micro client with service discovery, load balancing and streaming.
|
|
|
|
It complies with the [micro.Client](https://godoc.org/github.com/unistack-org/micro-client-http#Client) interface.
|
2017-01-01 21:39:05 +03:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
### Use directly
|
|
|
|
|
|
|
|
```go
|
2021-01-10 14:48:10 +03:00
|
|
|
import "github.com/unistack-org/micro-client-http"
|
2017-01-01 21:39:05 +03:00
|
|
|
|
|
|
|
service := micro.NewService(
|
|
|
|
micro.Name("my.service"),
|
|
|
|
micro.Client(http.NewClient()),
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
### Call Service
|
|
|
|
|
|
|
|
Assuming you have a http service "my.service" with path "/foo/bar"
|
|
|
|
```go
|
|
|
|
// new client
|
|
|
|
client := http.NewClient()
|
|
|
|
|
|
|
|
// create request/response
|
2017-01-01 22:33:24 +03:00
|
|
|
request := client.NewRequest("my.service", "/foo/bar", protoRequest{})
|
2017-01-01 22:33:44 +03:00
|
|
|
response := new(protoResponse)
|
2017-01-01 21:39:05 +03:00
|
|
|
|
|
|
|
// call service
|
|
|
|
err := client.Call(context.TODO(), request, response)
|
|
|
|
```
|
2017-01-01 21:41:07 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
or you can call any rest api or site and unmarshal to response struct
|
|
|
|
```go
|
|
|
|
// new client
|
|
|
|
client := client.NewClientCallOptions(http.NewClient(), http.Address("https://api.github.com"))
|
|
|
|
|
|
|
|
req := client.NewRequest("github", "/users/vtolstov", nil)
|
|
|
|
rsp := make(map[string]interface{})
|
|
|
|
|
|
|
|
err := c.Call(context.TODO(), req, &rsp, mhttp.Method(http.MethodGet))
|
|
|
|
```
|
|
|
|
|
2017-01-01 22:21:21 +03:00
|
|
|
Look at http_test.go for detailed use.
|
|
|
|
|
2017-01-01 22:20:53 +03:00
|
|
|
### Encoding
|
|
|
|
|
|
|
|
Default protobuf with content-type application/proto
|
|
|
|
```go
|
|
|
|
client.NewRequest("service", "/path", protoRequest{})
|
|
|
|
```
|
|
|
|
|
|
|
|
Json with content-type application/json
|
|
|
|
```go
|
|
|
|
client.NewJsonRequest("service", "/path", jsonRequest{})
|
|
|
|
```
|
|
|
|
|