From 31833d8e56c2c584adb7f7ef6f8e68714b864ab1 Mon Sep 17 00:00:00 2001 From: "liqiankun.1111" Date: Mon, 3 Jul 2023 15:04:58 +0800 Subject: [PATCH] feat: add response.ToJson --- .gitignore | 1 + httpclient.go | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/httpclient.go b/httpclient.go index f444bb0..a8e265e 100644 --- a/httpclient.go +++ b/httpclient.go @@ -156,6 +156,17 @@ func (this *Response) ToString() (string, error) { return string(bytes), nil } +func (this *Response) ToJson(v interface{}) error { + bytes, err := this.ReadAll() + if err != nil { + return err + } + if err = json.Unmarshal(bytes, v); err != nil { + return err + } + return nil +} + // Prepare a request. func prepareRequest(method string, url_ string, headers map[string]string, body io.Reader, options map[int]interface{}) (*http.Request, error) {