@@ -615,19 +615,19 @@ class EcpWebService {
615
615
616
616
// Execute request
617
617
try (Response response = client. newCall(requestBuilder. build()). execute()) {
618
- okhttp3.MediaType respContentType = response. body(). contentType()
619
- String subType = respContentType. subtype()?. toLowerCase()
618
+
619
+ ResponseBody responseBody = response. body()
620
+ okhttp3.MediaType respContentType = responseBody. contentType()
621
+ String mediaType = respContentType. type()?. toLowerCase()
620
622
def responseData
621
- String responseBody = response . body() . string()
622
- switch (subType) {
623
- case " json" :
623
+ if (isTextBased(mediaType)) {
624
+ responseData = responseBody . string()
625
+ if (mediaType . contains( ' json' )) {
624
626
ObjectMapper objectMapper = new ObjectMapper ()
625
- responseData = objectMapper. readValue(responseBody, Object )
626
- break
627
- case " text" :
628
- default :
629
- responseData = responseBody
630
- break
627
+ responseData = objectMapper. readValue(responseData, Object )
628
+ }
629
+ } else {
630
+ responseData = responseBody. byteStream()
631
631
}
632
632
633
633
result. statusCode = result. status = response. code()
@@ -641,6 +641,18 @@ class EcpWebService {
641
641
return result
642
642
}
643
643
644
+ private static boolean isTextBased (String contentType ) {
645
+ if (! contentType) {
646
+ log. error(" Missing content type" )
647
+ return false
648
+ }
649
+ String contentTypeLower = contentType. toLowerCase()
650
+ return contentTypeLower. contains(' json' ) ||
651
+ contentTypeLower. contains(' text' ) ||
652
+ contentTypeLower. contains(' xml' ) ||
653
+ contentTypeLower. contains(' graphql' )
654
+ }
655
+
644
656
/**
645
657
* Forwards a HTTP multipart/form-data request to ecodata.
646
658
* @param url the URL to forward to.
@@ -660,7 +672,7 @@ class EcpWebService {
660
672
}
661
673
662
674
Map postMultipart (String url , Map params , File file , String contentType , String originalFilename , String fileParamName = ' files' , boolean useToken = false , boolean userToken = false ) {
663
- RequestBody fileBody = RequestBody . create(okhttp3.MediaType . parse(contentType), file )
675
+ RequestBody fileBody = RequestBody . create(file, okhttp3.MediaType . parse(contentType))
664
676
postMultipart(url, params, fileBody, contentType, originalFilename, fileParamName, useToken, userToken)
665
677
}
666
678
0 commit comments