gohttp icon indicating copy to clipboard operation
gohttp copied to clipboard

Get a error when running test case

Open luochenxi opened this issue 6 years ago • 1 comments

Hi, @nahid Get a error when running test case:

  • Error
error 1 :
 # command-line-arguments
./test_post.go:22:11: resp.GetBodyWithUnmarshal undefined (type *gohttp.Response has no field or method GetBodyWithUnmarshal)

error 2 :
 # command-line-arguments
./test_async_post.go:28:17: op.GetBodyAsString undefined (type *gohttp.AsyncResponse has no field or method GetBodyAsString)
  • Code 1:
package main

import (
	"github.com/nahid/gohttp"
	"fmt"
)

func main() {
	req := gohttp.NewRequest()

	resp, err := req.
		FormData(map[string]string{"name": "Nahid"}).
		Post("https://httpbin.org/post")

	if err != nil {
		panic(err)
	}

	if resp.GetStatusCode() == 200 {
		var resps map[string]interface{}

		_ = resp.GetBodyWithUnmarshal(&resps)
		fmt.Println(resps["form"])
	}
}
  • Code 2:
package main

import (
	"github.com/nahid/gohttp"
	"fmt"
)

func main() {
	req := gohttp.NewRequest()
	ch := make(chan *gohttp.AsyncResponse)

	var users [3]string

	users[0] = "nahid"
	users[1] = "shipu"
	users[2] = "sujan"

	for i:=0; i<len(users); i++ {
		req.
		FormData(map[string]string{"user": users[i]}).
		AsyncPost("https://httpbin.org/post", ch)
	}


	for i:=0; i<len(users); i++ {
		op := <-ch

		fmt.Println(op.GetBodyAsString())
	}
}
  • Expectation test case work.

luochenxi avatar Apr 10 '19 03:04 luochenxi

The op type is the AsyncResponse.

type AsyncResponse struct {
    Resp *Response
    Err  error
}

I change this line with: fmt.Println(op.Resp.GetBodyAsString())

It can work.

yxhl avatar Dec 25 '19 02:12 yxhl