You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
Hello
I am implementing uploading a file to a specific page using rest api.
I am developing with Scriptrunner for Confluence.
-----------------------------------------------------------------------------------------
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import java.io.BufferedReader
import java.io.InputStreamReader
import javax.net.ssl.HttpsURLConnection
import java.net.URL
import javax.xml.bind.DatatypeConverter
import org.apache.log4j.Logger
public void PostAttachmentFile(def pageId, String fileName, Logger logger) {
byte[] message = ("admin:password").getBytes("UTF-8")
String basicAuth = DatatypeConverter.printBase64Binary(message)
int responseCode
try {
String baseURL = "https://wiki-dev.com/rest/api/content/${pageId}/child/attachment"
URL url = new URL(baseURL)
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection()
conn.setRequestMethod("POST")
conn.setRequestProperty("X-Atlassian-Token", "no-check")
conn.setRequestProperty("Authorization", "Basic " + basicAuth)
conn.setDoInput(true)
conn.setDoOutput(true)
def addAttr = new JsonSlurper().parseText(
"""
{
"file" : "${fileName}"
}
"""
)
logger.debug(addAttr)
def jsons = new JsonBuilder(addAttr).toPrettyString()
OutputStream os = conn.getOutputStream()
os.write(jsons.toString().getBytes("UTF-8"))
os.flush()
os.close()
responseCode = conn.getResponseCode()
} catch (Exception e) {
logger.debug(e.getMessage())
}
logger.debug(responseCode)
}
------------------------------------------------
PostAttachmentFile(pageId, "@/home/admin/test.csv", logger)
401 error
---------------------------------------------------
It is a linux (cent os) server and I get a 401 error even if I try to connect to the os where confluence is running and use curl.
I don't know what the problem is.