How can I make REST requests with HTTPBuilder that contain a german umlaut? If I try to do the same request using Postman it works. As soon as I try to do it with groovy HTTPBuilder using Scriptrunner for Jira, I always get "Bad Request"
Is there a way to be able to send the umlaut?
The code I am trying to use:
def http = new HTTPBuilder(<Endpoint>)
def token = ""
def zulieferer = Assets.search("objectSchemaId = 5 AND objectType = Lieferant")
zulieferer.each { asset ->
try {
def name = asset.getName()
def kundennummer = asset.getString("Kundennummer")
def kreditorNummer = asset.getString("Kreditor-Nummer")
def strasse = asset.getString("Straße")
def plz = asset.getString("PLZ")
def ort = asset.getString("Ort")
def fon = asset.getString("Telefon")
def hotlineFon = asset.getString("Hotline Telefon")
def hotlineMail = asset.getString("Hotline Mail")
def homepage = asset.getString("Homepage")
def consultant = asset.getString("Consultant")
def sales = asset.getString("Sales")
if (name != null) {
name.replace("–", "-")
}
if (kundennummer != null) {
kundennummer.replace("–", "-")
}
if (kreditorNummer != null) {
kreditorNummer.replace("–", "-")
}
if (strasse != null) {
strasse.replace("–", "-")
}
if (plz != null) {
plz.replace("–", "-")
}
if (ort != null) {
ort.replace("–", "-")
}
if (fon != null) {
fon.replace("–", "-")
}
if (hotlineFon != null) {
hotlineFon.replace("–", "-")
}
if (hotlineMail != null) {
hotlineMail.replace("–", "-")
}
if (homepage != null) {
homepage.replace("–", "-")
}
if (consultant != null) {
consultant.replace("–", "-")
}
if (sales != null) {
sales.replace("–", "-")
}
def contactString = "<p>Kundennummer: ${kundennummer}</p>\n" +
"<p>Kreditor-Nummer: ${kreditorNummer}</p>\n" +
"<p>Strasse: ${strasse}</p>\n" +
"<p>PLZ: ${plz}</p>\n" +
"<p>Ort: ${ort}</p>\n" +
"<p>Telefon: ${fon}</p>\n" +
"<p>Hotline Telefon: ${hotlineFon}</p>\n" +
"<p>Hotline Mail: ${hotlineMail}</p>\n" +
"<p>Homepage: ${homepage}</p>\n" +
"<p>Consultant: ${consultant}</p>\n" +
"<p>Sales: ${sales}</p>"
try {
def jsonBody = [tenant: 1, name: name, contact: contactString, resourcetype: "ThirdParty"]
http.request(POST, JSON) { req ->
headers.'Content-Type' = 'application/json'
headers.'Authorization' = "Bearer ${token}"
body = jsonBody
response.success = { resp, json ->
// log.warn(json)
}
}
} catch (Exception ex) {
log.warn("Asset: ${name} konnte nicht angelegt werden | " + ex)
}
just add http.encoders.charset = java.nio.charset.StandardCharsets.UTF_8
to force UTF-8 encoding
the following code sends data to a test server and receives back body:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovyx.net.http.ContentType
def http = new HTTPBuilder("http://httpbin.org/post")
http.encoders.charset = java.nio.charset.StandardCharsets.UTF_8
def contactString = "<p>Wörter werden großgeschrieben</p>"
def name = 'hält'
def jsonBody = [name: name, contact: contactString]
http.request(Method.POST, ContentType.JSON) { req ->
headers.'Content-Type' = 'application/json'
body = jsonBody
response.success = { resp, json ->
println json.data
//println "json=${new groovy.json.JsonBuilder(json).toPrettyString()}"
}
}
the code above prints:
{"name":"hält","contact":"<p>Wörter werden großgeschrieben<\/p>"}
if you have java.lang.NoClassDefFoundError: groovy/util/slurpersupport/GPathResult
consider this:
https://stackoverflow.com/a/71923258/1276664