It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

POST to Rest API Scriptrunner

Hi Guys, 

Im attempting to post to a rest api but not sure if im doing right with the header.

 

1) i get the token via the following (works ok)

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
import groovy.json.JsonOutput

def getToken(user, pw) {

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

response.success = { resp, JSON ->

token = JSON.get('IncognitoUms auth')
return token
}

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

}
}
}

2) posting with header

def addModem(key) {

def http = new HTTPBuilder('incognito/devices/')
oui = "test"
serial='test'
model = 'test'
productClass = 'test'
subscriberId = 'test'
manufacturer = 'test'
description = ''

requestBody = """{
"oui": "${oui}",
"productClass": "${productClass}",
"serial": "${serial}",
"subscriberId": "${subscriberId}",
"manufacturer": "${manufacturer}",
"description": "${description}",
"deviceModel": {
"id": "${model}"
}
}"""


headerRequest = """{"authorization": "IncognitoUms auth=${key}",
"content-type": "application/json}"""

http.request(PUT) {
requestContentType = ContentType.JSON
body = requestBody
header = headerRequest

response.success = { resp, JSON ->

token = JSON.get('IncognitoUms auth')
return token
}

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

}
}

2nd method getting 403 response. 

 

Not sure where i can go with this, any help is much appreciated. 

 

Cheers, 

Pon 

2 answers

1 accepted

2 votes
Answer accepted

First approach with things like this is to make sure you can make the request using curl, or postman or something, then turn it into code.

Other thing is you seem to be using two completely different URLs for making requests too, second one looks bogus.

Stylistically, it's much clearer and safe to write:

http.request(Method.PUT, ContentType.JSON) {
requestContentType = ContentType.JSON
body = [
productClass: 'test',
subscriberId: 'test',
]
...

rather than hand-rolling your own json from a string.

Ahh thank you Jamie! Much appreciated for your guidance. 

I ended up with the following:

def addModem(key, modemId) {

def http = new HTTPBuilder('http://incognito/devices')    

    http.request(POST, ContentType.JSON) {

        requestContentType = ContentType.JSON

        request.addHeader("authorization: IncognitoUms auth=${key}", "ContentType: application/json")

        body = [oui: "test",

               productClass: "test",

               serial: "test",

               subscriberId: "test",

               manufacturer: manufact,

               description: "",

               deviceModel: "{id: \"${modemId}\"}"]

        response.success = { resp, JSON ->

         return JSON
 
         }

         response.failure = { resp, JSON ->

            return JSON

        }

    }

}

 

Many thanks, 
Pon 

Hi @Pon Yuntawai

I am wondering how the second part is receiving the token fetched in the first part. Can you please share the full script that gets the token, inserts it in to the headers and makes a REST call ?  Thanks

 

--Raihan

Like Tadd Simmons likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Cloud Cafe Ep. 4: Listing your app on the Atlassian Marketplace

Hey y’all! Grab a cup of ☕ and check out the latest installment of Cloud Cafe – our video series about cloud app development on Atlassian. If you’re new to Cloud Cafe, check out the first few episo...

76 views 0 2
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you