Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP PUT in Groovy

Borja Tortosa
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 10, 2022

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

0 votes
aorlov August 12, 2022

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?

Borja Tortosa
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 12, 2022

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.
aorlov August 15, 2022

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