|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "encoding/json" |
| 4 | + "context" |
6 | 5 | "fmt" |
7 | | - "io/ioutil" |
8 | 6 | "log" |
9 | | - "net/http" |
10 | 7 |
|
| 8 | + "github.com/carlmjohnson/requests" |
11 | 9 | "github.com/palantir/stacktrace" |
12 | 10 | ) |
13 | 11 |
|
14 | 12 | func main() { |
15 | | - client := http.Client{} |
16 | | - for i := 0; i < 100; i++ { |
17 | | - payload, err := json.Marshal(map[string]string{ |
18 | | - "content": fmt.Sprintf("testing http api sample: [%d]", i), |
19 | | - "from": "+37259139660", |
20 | | - "to": "+37253517181", |
21 | | - }) |
| 13 | + for i := 0; i < 50; i++ { |
| 14 | + var responsePayload string |
| 15 | + err := requests. |
| 16 | + URL("/v1/messages/send"). |
| 17 | + Host("localhost:8000"). |
| 18 | + Scheme("http"). |
| 19 | + Header("x-api-key", "Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9HixkmBhVrYaB0NhtHpHgAWeTnL"). |
| 20 | + BodyJSON(&map[string]string{ |
| 21 | + "content": fmt.Sprintf("testing http api sample: [%d]", i), |
| 22 | + "from": "+37259139660", |
| 23 | + "to": "+37253517181", |
| 24 | + }). |
| 25 | + ToString(&responsePayload). |
| 26 | + Fetch(context.Background()) |
22 | 27 | if err != nil { |
23 | 28 | log.Fatal(stacktrace.Propagate(err, "cannot create json payload")) |
24 | 29 | } |
25 | | - response, err := client.Post("https://api.httpsms.com/v1/messages/send", "application/json", bytes.NewBuffer(payload)) |
26 | | - if err != nil { |
27 | | - log.Fatal(stacktrace.Propagate(err, "cannot perform http request")) |
28 | | - } |
29 | | - |
30 | | - if response.StatusCode != http.StatusOK { |
31 | | - log.Fatal(stacktrace.NewError(fmt.Sprintf("status code [%d] is different from expected [%d]", response.StatusCode, http.StatusOK))) |
32 | | - } |
33 | | - |
34 | | - responsePayload, err := ioutil.ReadAll(response.Body) |
35 | | - if err != nil { |
36 | | - log.Fatal(stacktrace.NewError(fmt.Sprintf("cannot read response"))) |
37 | | - } |
38 | 30 |
|
39 | | - log.Println(string(responsePayload)) |
| 31 | + log.Println(responsePayload) |
40 | 32 | } |
41 | 33 | } |
0 commit comments