Reputation: 11
New to Groovy, not experienced working with APIs. So my apologies if this is something general and not related with these topics. I just wish to understand it a bit.
I try to trigger a small script for a DELETE request towards the said API. The whole process doesn't work if I don't check the connection for any response-code.
import java.net.HttpURLConnection
// This sets up the connection to the api and delivers the request to delete identifier
def BaseUrl = "Someurl.com"
def personalAccessToken = "TOKEN"
def object= "id" //
def url = new URL("${BaseUrl}/apistuff/object")
def connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "DELETE"
connection.setRequestProperty("Authorization", "Bearer ${personalAccessToken}")
connection.setRequestProperty("Content-Type", "application/json")
connection.connect()
// This is the part I don't understand.
//Without mentioning the responseCode the script won't be executed to my expectations
if (connection.responseCode == 202) {}
connection.disconnect()
I expected the script to be able to delete the the object independently from the responseCode or anything related to that. If I leave the responseCode out, simply nothing happens. The API documentation mentions response 202 for success and 404 in case the object couldn't be found. But the status code itself doesn't matter as any I put there makes the script work.
I suspect this is might be more of a general issue with how these things work but I'd like to know more.
Upvotes: 1
Views: 20