如何用Groovy HTTPBuilder发送含德语变音符号的REST请求?
解决Groovy HTTPBuilder发送含德语变音符号请求的Bad Request问题
问题根源
- String不可变特性:代码中调用
replace后未将结果赋值回原变量,导致长破折号–未被替换为普通-,德语变音符号也未得到正确处理。 - 编码未明确指定:HTTPBuilder默认的JSON请求未显式设置UTF-8编码,而Postman会自动处理UTF-8编码,导致接口接收请求时解析失败。
修复步骤
- 对
replace操作的结果进行赋值(Groovy中String是不可变对象,replace不会修改原字符串)。 - 显式设置
Content-Type请求头为application/json;charset=utf-8,确保接口按UTF-8解析请求体。 - 可选:添加错误响应回调,打印接口返回的具体错误信息,精准定位问题。
修改后的完整代码
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") // 修复replace赋值问题,替换长破折号为普通短破折号 if (name != null) { name = name.replace("–", "-") } if (kundennummer != null) { kundennummer = kundennummer.replace("–", "-") } if (kreditorNummer != null) { kreditorNummer = kreditorNummer.replace("–", "-") } if (strasse != null) { strasse = strasse.replace("–", "-") } if (plz != null) { plz = plz.replace("–", "-") } if (ort != null) { ort = ort.replace("–", "-") } if (fon != null) { fon = fon.replace("–", "-") } if (hotlineFon != null) { hotlineFon = hotlineFon.replace("–", "-") } if (hotlineMail != null) { hotlineMail = hotlineMail.replace("–", "-") } if (homepage != null) { homepage = homepage.replace("–", "-") } if (consultant != null) { consultant = consultant.replace("–", "-") } if (sales != null) { sales = 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 -> // 显式指定UTF-8编码 headers.'Content-Type' = 'application/json;charset=utf-8' headers.'Authorization' = "Bearer ${token}" body = jsonBody response.success = { resp, json -> // log.warn(json) } // 添加错误响应处理,查看具体错误信息 response.failure = { resp, reader -> log.warn("请求失败,响应状态: ${resp.statusLine}, 响应内容: ${reader.text}") } } } catch (Exception ex) { log.warn("Asset: ${name} konnte nicht angelegt werden | " + ex) } } catch (Exception e) { log.warn("处理Asset时出错: " + e) } }
额外方案(若上述修改仍无效)
尝试手动序列化JSON,避免HTTPBuilder默认序列化的编码问题:
import groovy.json.JsonBuilder // 替换原请求部分代码 def jsonBuilder = new JsonBuilder(jsonBody) def jsonString = jsonBuilder.toString() http.request(POST) { req -> headers.'Content-Type' = 'application/json;charset=utf-8' headers.'Authorization' = "Bearer ${token}" body = jsonString.getBytes("UTF-8") response.success = { resp, json -> // log.warn(json) } response.failure = { resp, reader -> log.warn("请求失败,响应状态: ${resp.statusLine}, 响应内容: ${reader.text}") } }
内容的提问来源于stack exchange,提问作者N0m4n904
相关产品推荐
相关产品推荐

