post request with headers in scriptrunner

Oleksiy Ohmush June 5, 2019

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
Oleksiy Ohmush June 6, 2019

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 October 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
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.
June 5, 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
}
Oleksiy Ohmush June 5, 2019

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
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.
June 5, 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"
Oleksiy Ohmush June 5, 2019

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
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.
June 5, 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
Oleksiy Ohmush June 5, 2019

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