MCPcopy Create free account
hub / github.com/OpenAtomFoundation/SmartIDE / post

Method post

cli/pkg/common/http.go:251–289  ·  view source on GitHub ↗
(reqUrl string,
	reqParams map[string]interface{}, contentType string, files []UploadFile, headers map[string]string)

Source from the content-addressed store, hash-verified

249}
250
251func (target HttpClient) post(reqUrl string,
252 reqParams map[string]interface{}, contentType string, files []UploadFile, headers map[string]string) (
253 string, error) {
254 requestBody, realContentType := getReader(reqParams, contentType, files)
255 httpRequest, err := http.NewRequest("POST", reqUrl, requestBody)
256 if err != nil {
257 return "", err
258 }
259
260 // client
261 httpClient := getClient(reqUrl, target.TimeOut)
262
263 // 添加请求头
264 httpRequest.Header.Add("Content-Type", realContentType)
265 for k, v := range headers {
266 httpRequest.Header.Add(k, v)
267 }
268
269 // 发送请求
270 SmartIDELog.Debug(formatRequest(httpRequest, reqParams))
271 httpResponse, err := httpClient.Do(httpRequest)
272 if err != nil {
273 return "", err
274 }
275 responseBody, err := parsingResponse(httpResponse)
276 if err != nil {
277 return "", err
278 }
279 defer httpResponse.Body.Close()
280 if httpResponse.StatusCode != 200 {
281 return "", errors.New(httpResponse.Status)
282 }
283 //responseBody, err := io.ReadAll(httpResponse.Body)
284 if len(string(responseBody)) == 0 {
285 return "", errors.New("reponse body is empty")
286 }
287 return string(responseBody), err
288
289}
290
291func getClient(url string, timeout time.Duration) *http.Client {
292 _httpClient := &http.Client{

Callers 1

postRetryFunction · 0.80

Calls 6

getReaderFunction · 0.85
getClientFunction · 0.85
formatRequestFunction · 0.85
parsingResponseFunction · 0.85
DebugMethod · 0.80
DoMethod · 0.80

Tested by

no test coverage detected