MCPcopy Create free account
hub / github.com/asternic/wuzapi / convertHTTPResponse

Method convertHTTPResponse

stdio.go:507–547  ·  view source on GitHub ↗

convertHTTPResponse converts an HTTP response to a stdio response

(requestID ID, recorder *httptest.ResponseRecorder)

Source from the content-addressed store, hash-verified

505 }
506 // Set admin token header (for admin authentication)
507 if adminToken, ok := req.Params["adminToken"].(string); ok {
508 httpReq.Header.Set("Authorization", adminToken)
509 }
510
511 recorder := httptest.NewRecorder()
512 ss.server.router.ServeHTTP(recorder, httpReq)
513 ss.convertHTTPResponse(req.ID, recorder)
514}
515
516// convertHTTPResponse converts an HTTP response to a stdio response
517func (ss *stdioServer) convertHTTPResponse(requestID ID, recorder *httptest.ResponseRecorder) {
518 statusCode := recorder.Code
519 responseBody := recorder.Body.Bytes()
520
521 var responseData interface{}
522 if len(responseBody) > 0 {
523 if err := json.Unmarshal(responseBody, &responseData); err != nil {
524 // If it's not JSON, just use the raw string
525 responseData = string(responseBody)
526 }
527 }
528
529 success := statusCode >= 200 && statusCode < 300
530
531 if respMap, ok := responseData.(map[string]interface{}); ok {
532 // If it's already in wuzapi format, extract the data/error
533 if data, hasData := respMap["data"]; hasData {
534 ss.sendSuccess(requestID, statusCode, data)
535 return
536 }
537 if errMsg, hasError := respMap["error"]; hasError {
538 if errStr, ok := errMsg.(string); ok {
539 ss.sendError(requestID, statusCode, errStr)
540 return
541 }
542 }
543 ss.sendSuccess(requestID, statusCode, respMap)
544 return
545 }
546
547 // For non-JSON or simple responses
548 if success {
549 ss.sendSuccess(requestID, statusCode, responseData)
550 } else {

Callers 1

executeHTTPHandlerMethod · 0.95

Calls 2

sendSuccessMethod · 0.95
sendErrorMethod · 0.95

Tested by

no test coverage detected