Server returned HTTP response code: 400 using REST API

Jamshaid
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.
April 13, 2021

I am trying to run a script in the ScriptRunner console. The server on which Jira is running has two IPs that are internal & external IP. While using the internal IP in the script, I get the following error:

java.io.IOException: Server returned HTTP response code: 400 for URL: http://10.0.114.4:8082/rest/api/2/issue
at Script140.run(Script140.groovy:47)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://10.0.114.4:8082/rest/api/2/issue
at java_net_HttpURLConnection$getResponseCode.call(Unknown Source)
at Script140.run(Script140.groovy:44)

 

Script uses rest API to create an issue. The script works on another Jira correctly. There is no networking issue on my side. I can run a curl request on the server and the issue is created successfully. I am not getting any cause of the error. I also tried using the same network as the server to run Jira in the browser but the error is still there. Any ideas? Thanks

Here is my script

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.json.JsonSlurper
import groovy.json.StreamingJsonBuilder
import net.sf.json.groovy.JsonSlurper


def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
log.setLevel(Level.DEBUG)


def authString = "username:Password".getBytes().encodeBase64().toString()

def createIssueBody = """{"fields":
{
"summary": "Retail Bank Portal",
"issuetype": {
"id": "10201",
"subtask": false
},
"customfield_10402": "Login",
"customfield_10600": "Question:Does the business consider online channel significant for its success?\nAnswer:Yes\nQuestion:Are accounts locked upon a number of consecutive login failures?\nAnswer:No\nQuestion:Does this feature provide fund tranfer with other accounts?\nAnswer:No\nQuestion:Does the application use Single-Sign-on(SSO) beside its local login function?\nAnswer:No\nQuestion:Does the application have a self-serving function to allow users to recover forgotten password or user ID?\nAnswer:No\nQuestion:IF relational database in use?\nAnswer:No\n",
"customfield_10416": "{\"1800\":{\"fields\":[{\"id\":null,\"originId\":\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\",\"value\":\"Attack: password stuffing\\nControl: Implement technologies to monitor/block automated stuffing attack by patterns.\"},{\"id\":null,\"originId\":\"dbdc93c5-181e-4c98-bb52-4b19fba33143\",\"value\":\"372711e8-5305-4a75-a65f-ba79785ee96e\"}]},\"1799\":{\"fields\":[{\"id\":null,\"originId\":\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\",\"value\":\"Attack: obtain one-time-password by brute force\\nControl: limit retry of one-time-password to a lower threshold\"},{\"id\":null,\"originId\":\"dbdc93c5-181e-4c98-bb52-4b19fba33143\",\"value\":\"372711e8-5305-4a75-a65f-ba79785ee96e\"}]},\"1798\":{\"fields\":[{\"id\":null,\"originId\":\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\",\"value\":\"Attack: obtain one-time-password in compromised email\\nControl: use SMS to transmit one-time-password\"},{\"id\":null,\"originId\":\"dbdc93c5-181e-4c98-bb52-4b19fba33143\",\"value\":\"372711e8-5305-4a75-a65f-ba79785ee96e\"}]},\"configurationId\":\"4\",\"1804\":{\"fields\":[{\"id\":null,\"originId\":\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\",\"value\":\"Attack: compromise with failed login message\\nControl: Implement generic failed login message.\"},{\"id\":null,\"originId\":\"dbdc93c5-181e-4c98-bb52-4b19fba33143\",\"value\":\"372711e8-5305-4a75-a65f-ba79785ee96e\"}]},\"1803\":{\"fields\":[{\"id\":null,\"originId\":\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\",\"value\":\"Attack: compromise login with iframe\\nControl: Implement iframe busting code in login page so the page can not be iframed\"},{\"id\":null,\"originId\":\"dbdc93c5-181e-4c98-bb52-4b19fba33143\",\"value\":\"6f465090-ea0c-4ea7-a04f-bcebd85f9d4f\"}]},\"1802\":{\"fields\":[{\"id\":null,\"originId\":\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\",\"value\":\"Attack: weak password\\nControl: Implement strong password policy to prevent weak password in use\"},{\"id\":null,\"originId\":\"dbdc93c5-181e-4c98-bb52-4b19fba33143\",\"value\":\"6f465090-ea0c-4ea7-a04f-bcebd85f9d4f\"}]},\"1801\":{\"fields\":[{\"id\":null,\"originId\":\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\",\"value\":\"Attack: password brute force\\nControl: Implement account lockout policy\"},{\"id\":null,\"originId\":\"dbdc93c5-181e-4c98-bb52-4b19fba33143\",\"value\":\"6f465090-ea0c-4ea7-a04f-bcebd85f9d4f\"}]}}",
"project": {
"id": "10300",
"key": null,
"name": null
},
"customfield_10400": "Retail Bank Portal"}}"""

def connection = new URL("http://10.0.114.4:8082/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.getOutputStream().write(createIssueBody.getBytes("UTF-8"))
connection.connect()

//this is used only to see what Jira responses
def postRC = connection.getResponseCode();
println(postRC);
log.info(postRC)
def br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}

log.info(sb.toString())
String s=sb.toString();
def jsonString=s.split(",")
def keyString=jsonString[1]
def issueKeyArray=keyString.split(":")
def issueKey=issueKeyArray[1].replaceAll("\"","")

 

 

1 answer

1 accepted

1 vote
Answer accepted
Jamshaid
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.
April 14, 2021

It contained errors in JSON formatting which was causing all the issues. I reformatted JSON in the body and it worked perfectly. Used POSTMAN to format the JSON. got the formatted JSON from the code section of the POSTMAN request and posted that in the ScriptRunner console. Here is the fomratted JSON.

"{\"fields\":\n{\n \"summary\": \"Retail Bank Portal\",\n \"issuetype\": {\n \"id\": \"10201\",\n \"subtask\": false\n },\n \"customfield_10402\": \"Login\",\n \"customfield_10600\": \"Question:Does the business consider online channel significant for its success?\\nAnswer:Yes\\nQuestion:Are accounts locked upon a number of consecutive login failures?\\nAnswer:No\\nQuestion:Does this feature provide fund tranfer with other accounts?\\nAnswer:No\\nQuestion:Does the application use Single-Sign-on(SSO) beside its local login function?\\nAnswer:No\\nQuestion:Does the application have a self-serving function to allow users to recover forgotten password or user ID?\\nAnswer:No\\nQuestion:IF relational database in use?\\nAnswer:No\\n\",\n \"customfield_10416\": \"{\\\"1800\\\":{\\\"fields\\\":[{\\\"id\\\":null,\\\"originId\\\":\\\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\\\",\\\"value\\\":\\\"Attack: password stuffing\\\\nControl: Implement technologies to monitor/block automated stuffing attack by patterns.\\\"},{\\\"id\\\":null,\\\"originId\\\":\\\"dbdc93c5-181e-4c98-bb52-4b19fba33143\\\",\\\"value\\\":\\\"372711e8-5305-4a75-a65f-ba79785ee96e\\\"}]},\\\"1799\\\":{\\\"fields\\\":[{\\\"id\\\":null,\\\"originId\\\":\\\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\\\",\\\"value\\\":\\\"Attack: obtain one-time-password by brute force\\\\nControl: limit retry of one-time-password to a lower threshold\\\"},{\\\"id\\\":null,\\\"originId\\\":\\\"dbdc93c5-181e-4c98-bb52-4b19fba33143\\\",\\\"value\\\":\\\"372711e8-5305-4a75-a65f-ba79785ee96e\\\"}]},\\\"1798\\\":{\\\"fields\\\":[{\\\"id\\\":null,\\\"originId\\\":\\\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\\\",\\\"value\\\":\\\"Attack: obtain one-time-password in compromised email\\\\nControl: use SMS to transmit one-time-password\\\"},{\\\"id\\\":null,\\\"originId\\\":\\\"dbdc93c5-181e-4c98-bb52-4b19fba33143\\\",\\\"value\\\":\\\"372711e8-5305-4a75-a65f-ba79785ee96e\\\"}]},\\\"configurationId\\\":\\\"4\\\",\\\"1804\\\":{\\\"fields\\\":[{\\\"id\\\":null,\\\"originId\\\":\\\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\\\",\\\"value\\\":\\\"Attack: compromise with failed login message\\\\nControl: Implement generic failed login message.\\\"},{\\\"id\\\":null,\\\"originId\\\":\\\"dbdc93c5-181e-4c98-bb52-4b19fba33143\\\",\\\"value\\\":\\\"372711e8-5305-4a75-a65f-ba79785ee96e\\\"}]},\\\"1803\\\":{\\\"fields\\\":[{\\\"id\\\":null,\\\"originId\\\":\\\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\\\",\\\"value\\\":\\\"Attack: compromise login with iframe\\\\nControl: Implement iframe busting code in login page so the page can not be iframed\\\"},{\\\"id\\\":null,\\\"originId\\\":\\\"dbdc93c5-181e-4c98-bb52-4b19fba33143\\\",\\\"value\\\":\\\"6f465090-ea0c-4ea7-a04f-bcebd85f9d4f\\\"}]},\\\"1802\\\":{\\\"fields\\\":[{\\\"id\\\":null,\\\"originId\\\":\\\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\\\",\\\"value\\\":\\\"Attack: weak password\\\\nControl: Implement strong password policy to prevent weak password in use\\\"},{\\\"id\\\":null,\\\"originId\\\":\\\"dbdc93c5-181e-4c98-bb52-4b19fba33143\\\",\\\"value\\\":\\\"6f465090-ea0c-4ea7-a04f-bcebd85f9d4f\\\"}]},\\\"1801\\\":{\\\"fields\\\":[{\\\"id\\\":null,\\\"originId\\\":\\\"f3f83c1b-f88f-4ce9-9e92-ea9c9be079a5\\\",\\\"value\\\":\\\"Attack: password brute force\\\\nControl: Implement account lockout policy\\\"},{\\\"id\\\":null,\\\"originId\\\":\\\"dbdc93c5-181e-4c98-bb52-4b19fba33143\\\",\\\"value\\\":\\\"6f465090-ea0c-4ea7-a04f-bcebd85f9d4f\\\"}]}}\",\n \"project\": {\n \"id\": \"10300\",\n \"key\": null,\n \"name\": null\n },\n \"customfield_10400\": \"Retail Bank Portal\"\n}\n}"

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events