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?
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}'
}
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for reply!
I tried, but i guess i do something wrong
And it contain error if i try to use {} in body
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) {...}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.