Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,369,454
Community Members
 
Community Events
168
Community Groups

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

Atlassian Community Events