MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / main

Function main

examples/http-request/main.go:73–140  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

71}
72
73func main() {
74 log.SetLevel(log.InfoLevel)
75
76 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
77 defer cancel()
78
79 core := coreauth.NewManager(nil, nil, nil)
80 core.RegisterExecutor(EchoExecutor{})
81
82 auth := &coreauth.Auth{
83 ID: "demo-echo",
84 Provider: providerKey,
85 Attributes: map[string]string{
86 "api_key": "demo-api-key",
87 },
88 }
89
90 // Example 1: Build a prepared request and execute it using your own http.Client.
91 reqPrepared, errReqPrepared := core.NewHttpRequest(
92 ctx,
93 auth,
94 http.MethodGet,
95 "https://httpbin.org/anything",
96 nil,
97 http.Header{"X-Example": []string{"prepared"}},
98 )
99 if errReqPrepared != nil {
100 panic(errReqPrepared)
101 }
102 respPrepared, errDoPrepared := http.DefaultClient.Do(reqPrepared)
103 if errDoPrepared != nil {
104 panic(errDoPrepared)
105 }
106 defer func() {
107 if errClose := respPrepared.Body.Close(); errClose != nil {
108 log.Errorf("close response body error: %v", errClose)
109 }
110 }()
111 bodyPrepared, errReadPrepared := io.ReadAll(respPrepared.Body)
112 if errReadPrepared != nil {
113 panic(errReadPrepared)
114 }
115 fmt.Printf("Prepared request status: %d\n%s\n\n", respPrepared.StatusCode, bodyPrepared)
116
117 // Example 2: Execute a raw request via core.HttpRequest (auto inject + do).
118 rawBody := []byte(`{"hello":"world"}`)
119 rawReq, errRawReq := http.NewRequestWithContext(ctx, http.MethodPost, "https://httpbin.org/anything", bytes.NewReader(rawBody))
120 if errRawReq != nil {
121 panic(errRawReq)
122 }
123 rawReq.Header.Set("Content-Type", "application/json")
124 rawReq.Header.Set("X-Example", "executed")
125
126 respExec, errDoExec := core.HttpRequest(ctx, auth, rawReq)
127 if errDoExec != nil {
128 panic(errDoExec)
129 }
130 defer func() {

Callers

nothing calls this directly

Calls 6

RegisterExecutorMethod · 0.95
NewHttpRequestMethod · 0.95
HttpRequestMethod · 0.95
DoMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected