Forums

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

Create Issue with Groovy Script via REST API

Coco _El mejor_
Contributor
July 14, 2019

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()   

 

2 answers

1 accepted

3 votes
Answer accepted
Coco _El mejor_
Contributor
July 15, 2019

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());
}
Moses Thomas
Community Champion
July 4, 2022

@Coco _El mejor_ 

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

Moses Thomas
Community Champion
July 6, 2022

OK i  fix  the problem the issue is with the personal token, i  generated it from user profile and  it work!

Like Coco _El mejor_ likes this
0 votes
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 15, 2019

Hi @Coco _El mejor_ 

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

Coco _El mejor_
Contributor
July 15, 2019

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());
}
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 15, 2019

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

Coco _El mejor_
Contributor
July 15, 2019

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

Suggest an answer

Log in or Sign up to answer