Send post http request via scriptrunner post function

Hello :)

 

Im wondering is there a way to send post http request via the scriptrunner postfunction feature?

 

The current script i have is JAVA:

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\r\n\"username\":\"USERNAME\",\r\n\"password\":\"PASSWORD\"\r\n}");
Request request = new Request.Builder()
  .url("http://IP ADDRESS/authentication")
  .post(body)
  .addHeader("content-type", "application/json")
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "dabd56ae-0efb-db67-72fe-657b5682a466")
  .build();

Response response = client.newCall(request).execute();

Anyone care to help to convert into groovyscript.

 

Many thanks!

Pon

 

 

1 answer

1 accepted

Hi Pon:

Doing a search on this I found a community question that might be helpful to you.

Here you will find a descriptive example on how to do this with Jira and Scriptrunner. If this isn't working for you, give me a thorough example on what you want to do and I will try to provide a custom script for you.

Cheers!

Dyelamos

 

Hey Daniel, 

Thanks for the reply!

Im quite new to this, basically what im trying to do is call rest api to return a sucessful response which: 

 

i attempted the above, i dont see where i can place the auth/payload when sending the request, i tried searching also around and gotten around this far:

 

package com.hybris.activity

import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient

 def activitiRestClient = new RESTClient("http://10.150.14.164:8090/authentication")
        activitiRestClient.auth.basic "USERNAME", "PASSWORD"

def response = activitiRestClient.get(
                   path: "/devices"
                )
println response.data.toString(2)

I have a python 3 code which is as follows (and works in python):

import http.client

conn = http.client.HTTPConnection("10.150.14.164:8090")

payload = "{\r\n\"username\":\"USERNAME\",\r\n\"password\":\"PASSWORD\"\r\n}"

headers = {
    'content-type': "application/json",
    'cache-control': "no-cache",
    'postman-token': "aace72a6-d5d2-1f44-23bf-4a10b6306b4c"
    }

conn.request("POST", "/authentication", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

which im trying to convert over to groovy script. 

Any guidance would be very helpful :)

 

Thanks, 

Pon

 

 

Hi Pon:

In this link you will find official documentation as to how to use httpBuilder to send a post request. That is the utility that we use to build post request in Adaptavist.

Cheers

DYelamos

 

Thanks Daniel :) got it working. 

 

It was so simple all along:

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper

def http = new HTTPBuilder('http://IP/authentication')
http.request(POST) {
    requestContentType = ContentType.JSON
    body =  [username: 'USERNAME', password: 'PASSWORD']

        response.success = { resp, JSON ->

  return JSON
            
    }
    
        response.failure = { resp ->
        return "Request failed with status ${resp.status}"
            
            }
    }

Glad you did mate! Thanks for the upvote and good luck in future programming! 

Cheers

 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

178 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you