My idea is create issues via rest api with scriptrunner. It seems to be Error 400 (Bad Request). I tried the data json variable in Postman and works, so i guess there is an error in the HEAD. Can someone help me?
import groovy.json.StreamingJsonBuilder
def authString = "user:passw".getBytes().encodeBase64().toString()
def body_req = [
"fields": [
"project" : [ "key" : "TR" ],
"summary" : "test",
"description" : "test",
"issuetype" : [ "name" : "Incident" ]]
]
def baseURL = "<<JIRA_LINK>>/rest/api/2/issue/"
URL url
url = new URL(baseURL)
HttpURLConnection connection = (HttpURLConnection)url.openConnection()
connection.setRequestMethod( "POST" )
connection.setRequestProperty( "Authorization", "Basic ${authString}" )
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) }
connection.connect()
Finally I made it. Here is the final code :
import groovy.json.StreamingJsonBuilder
def authString = "user:passw".getBytes().encodeBase64().toString()
def body_req = '''{
"fields": {
"project" : { "key" : "TR" },
"issuetype" : { "name" : "Incident" },
"summary" : "test",
"description" : "test"}
}'''
def connection = new URL("<<JIRA-URL>>/rest/api/2/issue/").openConnection() as HttpURLConnection
connection.setRequestMethod( "POST" )
connection.setRequestProperty( "Authorization", "Basic ${authString}" )
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
/*connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) }*/
connection.getOutputStream().write(body_req.getBytes("UTF-8"))
connection.connect()
def postRC = connection.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(connection.getInputStream().getText());
}
On JIRA server after generating the API token then i have been trying but i got error code 405 meaning method not allowed. do you have any clue ? when i tried in post man it works perfectly. Auth string is the accessing token.
import groovy.json.StreamingJsonBuilder
def authString = "3pVGn3ndeSN1HhSxr1KHqD3DqItlp2vc"
/*def body = '''{
"fields": {
"project" : { "key" : "EUL" },
"issuetype" : { "name" : "Task" },
"summary" : "test2",
"description" : "test2"}
}'''
*/
def connection = new URL("http://localhost:5433/rest/api/2/customFields").openConnection() as HttpURLConnection
connection.setRequestMethod( "GET" )
connection.setRequestProperty( "Authorization", "Bearer ${authString}" )
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
//connection.getOutputStream().write(body.getBytes("UTF-8"))
connection.connect()
def ResponseCode = connection.getResponseCode();
println(ResponseCode);
if(ResponseCode.equals(200)) {
println(connection.getInputStream().getText());
}
Kind regards,
Moses
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OK i fix the problem the issue is with the personal token, i generated it from user profile and it work!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've noticed one thing potentially wrong with your code, but that would give a 401, not a 400. Anyway, you need to change from using password to an API token, as per this
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It seems API token is cloud-only, I can´t use it in server version. I think there is a problem with HttpURLConnection class. As I see here, i made some changes in the code :
import groovy.json.StreamingJsonBuilder
def authString = "user:passw".getBytes().encodeBase64().toString()
def body_req = '''{"fields":{
"project" : { "key" : "TR" },
"issuetype" : { "name" : "Incident" },
"summary" : "test",
"description" : "test"}
}'''
def connection = new URL("<<JIRA_URL>>").openConnection() as HttpURLConnection
connection.setRequestMethod( "POST" )
connection.setRequestProperty( "Authorization", "Basic ${authString}" )
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.getOutputStream().write(body_req.getBytes("UTF-8"))
connection.connect()
//this is used only to see what Jira responses
def postRC = connection.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(connection.getInputStream().getText());
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry @Coco _El mejor_ , my fault for not checking that it was server. I don't have access to a server version, and I don't know Groovy, I use C#.
Comparing what I have in C# to what you have, I notice that my authorisation is added as a Header method on my request - I don't know if HttpURLConnection has something similar? Otherwise I'm all out of ideas because I have no knowledge of Groovy
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
with setRequestProperty() , the paramethers are added in the header of the request. Just only a question: Now i´m using groovy. As far as I know Scriptrunner use Groovy only for develop scripts. Do you have another scripting alternative?
Cheers and thanks for response
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.