perbaikan decompres
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"api-service/internal/config"
|
||||
@@ -16,6 +17,18 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// cleanResponse removes invalid characters and BOM from the response string
|
||||
func cleanResponse(resp string) string {
|
||||
// Remove UTF-8 BOM
|
||||
resp = strings.TrimPrefix(resp, "\xef\xbb\xbf")
|
||||
resp = strings.TrimPrefix(resp, "\ufeff")
|
||||
// Remove null characters
|
||||
resp = strings.ReplaceAll(resp, "\x00", "")
|
||||
// Trim whitespace
|
||||
resp = strings.TrimSpace(resp)
|
||||
return resp
|
||||
}
|
||||
|
||||
// VClaimService interface for VClaim operations
|
||||
type VClaimService interface {
|
||||
Get(ctx context.Context, endpoint string, result interface{}) error
|
||||
@@ -172,6 +185,12 @@ func (s *Service) processResponse(res *http.Response) (*ResponDTOVclaim, error)
|
||||
return finalResp, nil
|
||||
}
|
||||
|
||||
// Check if response looks like HTML or error message (don't try to decrypt)
|
||||
if strings.HasPrefix(respMentah.Response, "<") || strings.Contains(respMentah.Response, "error") {
|
||||
finalResp.Response = respMentah.Response
|
||||
return finalResp, nil
|
||||
}
|
||||
|
||||
// Decrypt response
|
||||
consID, secretKey, _, tstamp, _ := s.config.SetHeader()
|
||||
decryptionKey := consID + secretKey + tstamp
|
||||
@@ -190,6 +209,8 @@ func (s *Service) processResponse(res *http.Response) (*ResponDTOVclaim, error)
|
||||
|
||||
// Try to unmarshal decrypted response as JSON
|
||||
if respDecrypt != "" {
|
||||
// Clean the decrypted response
|
||||
respDecrypt = cleanResponse(respDecrypt)
|
||||
if err := json.Unmarshal([]byte(respDecrypt), &finalResp.Response); err != nil {
|
||||
// If JSON unmarshal fails, store as string
|
||||
log.Warn().Err(err).Msg("Failed to unmarshal decrypted response, storing as string")
|
||||
|
||||
Reference in New Issue
Block a user