Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

post request with headers in scriptrunner

Hello 

I`m trying to use scriptrunner to reset cache in cloudflare.

I have api key, zone id, login in customfields in issue.

 

customfields : api, zone, login

def http = new HTTPBuilder('https://api.cloudflare.com/client/v4/zones/$zone/purge_cache')
http.request(POST) {
requestContentType = ContentType.JSON
body = [{"purge_everything":true}]

response.success = { resp, JSON ->

return JSON
}

How to add headers and what libs should I import?

2 answers

1 accepted

1 vote
Answer accepted

Actually i find a solution. Problem was in invalid URL

def http = new HTTPBuilder('https://api.cloudflare.com/client/v4/zones/'+zone+'/purge_cache')
http.request(Method.POST) {
headers."X-Auth-Email" = "$mlogin"
headers."X-Auth-Key" = "$api"
requestContentType = ContentType.JSON
body = '{"purge_everything":true}'
}
Stefano Coletta
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
Oct 20, 2023

Is there a way to set these headers in the HTTPBuilder object?
In my code I have to do multiple requests and I would like to set them only at the beginning.

Thanks :)

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 05, 2019

Try something like this (2 variants proposed)

No additional import required, it's all part of HTTPBuilder

def http = new HTTPBuilder('https://api.cloudflare.com/client/v4/zones/$zone/purge_cache')

def headerKey= "something"
def headerValue = "somethingElse"

http.request(POST) {
headers."keyForYourHeader" = "value for your header"
headers[headerKey] = headerValue
   requestContentType = ContentType.JSON
    body = [{"purge_everything":true}]
    response.success = { resp, JSON ->

    return JSON
}

Thanks for reply!

I tried, but i guess i do something wrong

And it contain error if i try to use {} in body

Снимок экрана 2019-06-05 в 23.37.08.pngСнимок экрана 2019-06-05 в 23.36.54.png

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 05, 2019

If you want to store the header key in a variable, you must assign it to the headers object using the square bracket format.

When you do headers.headerKey, you are trying to create a header with the literal key of "headerKey"

 

So try:

headers[headerKey] = mlogin 

(or is mlogin the literal value for the header? in this case, include it with quotes: "mlogin")

 

But more simply, to set the two headers you showed in the screenshot, there is no need to first declare them as variables, just include:

headers."X-Auth-Email" = "mlogin"
headers."X-Auth-Key" = "api"

I tried, guess i still need to include some libs

 

def http = new HTTPBuilder('https://api.cloudflare.com/client/v4/zones/$zone/purge_cache')


http.request(POST) {

headers."X-Auth-Email" = "mlogin"
headers."X-Auth-Key" = "api"
requestContentType = ContentType.JSON
body = ["purge_everything":true]
response.success = { resp, JSON ->

return JSON}
}

 


No such property: POST for class: Script696

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 05, 2019

I was assuming you already had the HTTPBuilder imports... and I only used the code you posted.

These are what I use:

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

 Then you request is done with

http.request(Method.POST) {...}
Like Oleksiy Ohmush likes this

Thanks, now POST method works but still bad requestСнимок экрана 2019-06-06 в 08.34.58.png

Suggest an answer

Log in or Sign up to answer