Skip to content

Commit

Permalink
Merge pull request #7 from mercedes-benz/api_requests_update
Browse files Browse the repository at this point in the history
fix bug - print results when status code is notOk
  • Loading branch information
fafa0803 authored Apr 24, 2023
2 parents c8b37b9 + 9b4e0a2 commit 235261c
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions pkg/helper/apiRequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,22 @@ func DiscoApiMultipartPost(url string, completeFilename string) string {
fmt.Println(err)
os.Exit(1)
}

statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
fmt.Println("Operation failed with status", resp.Status)
os.Exit(1)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error on read response of url " + url)
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Response from Host " + url)

statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
fmt.Println("Operation failed with status:", resp.Status)
fmt.Println(string(body))
os.Exit(1)
}

return string(body)
}

Expand All @@ -95,21 +97,22 @@ func DiscoApiPost(url string, v interface{}) string {
fmt.Println("Error on requesting url " + url)
os.Exit(1)
}

statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
fmt.Println("Operation failed with status", resp.Status)
os.Exit(1)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println("Response from Host " + url)

statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
fmt.Println("Operation failed with status:", resp.Status)
fmt.Println(string(body))
os.Exit(1)
}

return string(body)
}

Expand All @@ -129,20 +132,21 @@ func DiscoApiGet(url string) string {
fmt.Println(err)
os.Exit(1)
}

statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
fmt.Println("Operation failed with status", resp.Status)
os.Exit(1)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println("Response from Host " + url)

statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
fmt.Println("Operation failed with status:", resp.Status)
fmt.Println(string(body))
os.Exit(1)
}

return string(body)
}

0 comments on commit 235261c

Please sign in to comment.