Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

HTTP PUT in Groovy

Good evening,

 

I have been trying to send a PUT request to the Confluence API using a groovy script, It works fine if I use curl but I am not able to do it with the following code. I always get a 403.


 

def putFunction(){
    try {
    def put    = new URL("https://URLREQUESTED.atlassian.net/wiki/rest/api/content/3290431880").openConnection();
    def auth    = "username:token".bytes.encodeBase64()

    def message = '<p>TEST TEST TEST</p>'

    put.setRequestMethod("PUT")
    put.setDoOutput(true)
    put.setRequestProperty("Content-Type", "application/json")
    put.setRequestProperty("Authorization", "Basic" + "${auth}")
    put.getOutputStream().write(message.getBytes("UTF-8"));

    def putRC = put.getResponseCode();
   
    println(putRC);
   
        if (putRC.equals(200)) {
            println(put.getInputStream().getText());
        }

    }   catch (Exception e) {
    e.printStackTrace()
    }
}

What am I doing wrong here?  I've done a get with the same method and it works fine.
Thanks for your help, best regards.

1 answer

Hi @Borja Tortosa . It's hard to say. What is the error code? What is the error message? Did you provide all necessary parameters for the  PUT request?

I am getting a 403, but I did provide the authorization:

def auth         = "admin@admin:token".bytes.encodeBase64()
I get no error message, just the code :S, maybe the headers are incomplete?? 
I've tried several solutions that worked with curl but not with the custom method.

First thoughts:

1. You don't have a space in the line 

"Basic" + "${auth}"

 after "Basic". Should be "Basic " (space before last quote).

Right now you have

Basicusername:token

Should be

Basic username:token

2. Are you sure about  ?

def auth    = "username:token".bytes.encodeBase64()

  Did you try?

def auth    = "username:token"

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events