Sibert
(Sibert)
December 25, 2020, 10:12am
1
I am trying to understand the web - API stuff. So I created a simple API that I called from a client. This is really basic and I have no clue what I am doing.
The Client
package main
import (
"fmt"
"net/http"
)
func main() {
reply, _ := http.Get("http://127.0.0.1:5051/test")
reply.Body.Close()
fmt.Println(reply.Body)
}
The Server
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/", get)
http.ListenAndServe(":5051", nil)
}
func get(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
}
The result in the client is this. Not “Hello World”.
&{0xc0001c62c0 {0 0} true 0x1234aa0 0x1234a30}
How can I convert this to a string? Any other tips or links are welcome.
NobbZ
(Norbert Melzer)
December 25, 2020, 10:19am
2
The Body
is a reader, you need to read the actual body contents from it.
Sibert
(Sibert)
December 25, 2020, 10:20am
3
I think I found one solution:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
reply, _ := http.Get("http://127.0.0.1:5051/test")
defer reply.Body.Close()
data, _ := ioutil.ReadAll(reply.Body)
response := string(data)
fmt.Println(response)
}
Can it be done in another way?
NobbZ
(Norbert Melzer)
December 25, 2020, 12:19pm
4
Yes, you could use the io.Reader
methods directly to avoid the potentially big memory allocation for the full body.
fea_kafea
(Kiri_139)
December 25, 2020, 12:29pm
5
Sibert:
http.Get
I’ll talk about the request.
Don’t send too many server requests! It will overload the server side! Do it at your own risk…
You can send slightly more detailed requests with
https//golang.org/pkg/net/http/#Client
I’ve never used it from here down. sorry
If you are skilled in the go language, you may want to try the net package. (I’ve never used one. )
net package - net - Go Packages
There is a suggestion to use cgo to use c language requests
Knowledge of the c language is required. (I’ve never used one. )
*The following links are in Japanese It looks like you can do it with the following link (I’ve never done it before)
**
**
I’m glad I can be of some help.
Sibert
(Sibert)
December 25, 2020, 3:33pm
6
How do I implement this?
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
reply, _ := http.Get("http://127.0.0.1:5051/test")
defer reply.Body.Close()
response := io.Reader(reply.Body)
fmt.Println(response)
}
The result is almost the same:
&{0xc000076040 {0 0} false 0x1234aa0 0x1234a30}
NobbZ
(Norbert Melzer)
December 25, 2020, 4:01pm
7
io.Reader
is an interface. It’s implementors provide a Read()
which takes an output buffer and returns number of bytes read into the buffer and an error.
Sibert
(Sibert)
December 26, 2020, 9:44am
8
I have tested with the Client. What is the benefits of using this instead of http.Get?
Client
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", endpoint)
err := http.ListenAndServe(":5050", nil)
if err != nil {
fmt.Println(err)
}
}
func endpoint(w http.ResponseWriter, r *http.Request) {
path := strings.Trim(r.URL.Path, "/")
client := &http.Client{}
url := ("http://94.237.92.101:6061/" + path)
req, _ := http.NewRequest(http.MethodGet, url, nil)
res, _ := client.Do(req)
if res.Body != nil {
defer res.Body.Close()
}
body, _ := ioutil.ReadAll(res.Body)
w.Write(body)
}
Server with 2 endpoints
package main
import (
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", get)
http.ListenAndServe(":6061", nil)
}
func get(w http.ResponseWriter, r *http.Request) {
path := strings.Trim(r.URL.Path, "/")
switch path {
case "usrs":
w.Write([]byte("user list"))
case "posts":
w.Write([]byte("post list"))
default:
w.Write([]byte("Unknown"))
}
}
http://94.237.92.101:6061
http://94.237.92.101:6061/usrs
http://94.237.92.101:6061/posts
NobbZ
(Norbert Melzer)
December 26, 2020, 12:06pm
9
Client
enables you to use more advanced features like connection pooling, but it’s fine to start with Get
and Post
1 Like
system
(system)
Closed
March 26, 2021, 12:06pm
10
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.