2

I am trying access one of the Jenkins job's log using groovy script. but getting 403 error. How do I pass the credential to login in below code?

def jsonStr1 = new URL(myEnvUrl+"warnings40Result/api/json?pretty=true").getText()
dimwittedanimal
  • 656
  • 1
  • 13
  • 29

2 Answers2

0

You are getting HTTP 403 which stands for Unauthorized attempt.

Possibly there is a login page of Jenkins, you should include it to access your next page. page. Have a check following link:

Groovy built-in REST/HTTP client?

yılmaz
  • 1,818
  • 13
  • 15
  • Still not able to access without configure Login Credentials into the groovy code **def jsonStr1 = new URL(myEnvUrl+"warnings40Result/api/json?pretty=true").getText()** I tried all the solution of url: [link](https://stackoverflow.com/questions/25692515/groovy-built-in-rest-http-client) Anyone having idea how to parse this by passing credential’s . – user1671258 Aug 07 '18 at 12:10
  • Can you share your code about logging in to Jenkins? What have you did so far? – yılmaz Aug 07 '18 at 16:50
  • **My code:** def warningJsonUrl = EnvBuildUrl+"warnings40Result/api/json?token=4eca462899e426937a94006a20561011" def authString = "admin1:admin1".getBytes().encodeBase64().toString() def conn = warningJsonUrl.toURL().openConnection() conn.setRequestProperty( "Authorization", "Basic ${authString}" ) if( conn.responseCode == 200 ) { println("code:"+conn.responseCode) def textJsonObj = new JsonSlurper().parseText(conn.content.text) } how i will parse as text? – user1671258 Aug 09 '18 at 06:29
  • @user1671258 what is output of following codes? println(conn.content) and println(conn.content.text) – yılmaz Aug 12 '18 at 01:28
0
def jsonStr1 = new URL(myEnvUrl+"warnings40Result/api/json?pretty=true").getText()

I tried all the solution of url:
https://stackoverflow.com/questions/25692515/groovy-built-in-rest-http-client

i think without Login Credentials code we can't access 'jsonStr1'. so i tried below code, now i am able to access but while parsing the value its giving error: 

code:200
[PostBuildScript] - Problem occurred: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. 


my code:
def  warningJsonUrl = EnvBuildUrl+"warnings40Result/api/json?token=4eca462899e426937a94006a20561011"

def authString = "admin1:admin1".getBytes().encodeBase64().toString()

def conn = warningJsonUrl.toURL().openConnection()
conn.setRequestProperty( "Authorization", "Basic ${authString}" )
if( conn.responseCode == 200 ) {
println("code:"+conn.responseCode)

def textJsonObj = new JsonSlurper().parseText(conn.content.text) 
}
how i will parse as text?
Raj
  • 51
  • 4