Fetch result from API server

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.

The Body is a reader, you need to read the actual body contents from it.

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?

Yes, you could use the io.Reader methods directly to avoid the potentially big memory allocation for the full body.

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. :sweat:)
https://golang.org/pkg/net/#Dial

  • There is a suggestion to use cgo to use c language requests

Knowledge of the c language is required. (I’ve never used one. :sweat:)
*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.

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}

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.

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

Client enables you to use more advanced features like connection pooling, but it’s fine to start with Get and Post

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.