-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
杨赫然
committed
Jul 24, 2024
1 parent
b4578da
commit 8267b84
Showing
6 changed files
with
153 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
var HttpReqContext, HttpReqCancel = context.WithCancel(context.Background()) | ||
|
||
func HttpCommon(method, url string, header map[string][]string, reader io.Reader) (int, []byte, error) { | ||
req, err := http.NewRequestWithContext(HttpReqContext, method, url, reader) | ||
if err != nil { | ||
return -1, nil, err | ||
} | ||
req.Header = header | ||
|
||
rsp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return -1, nil, err | ||
} | ||
defer rsp.Body.Close() | ||
|
||
if rsp.StatusCode == http.StatusNotFound { | ||
return rsp.StatusCode, nil, fmt.Errorf("url %s not found", url) | ||
} | ||
body, err := io.ReadAll(rsp.Body) | ||
if err != nil { | ||
return rsp.StatusCode, nil, err | ||
} | ||
|
||
return rsp.StatusCode, body, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters