[英]Groovy POST large content using HttpURLConnection
我有以下代碼用於向 REST API 端點發出 POST 請求以更新 SAP 應用程序數據。
#!/usr/bin/env groovy
import groovy.json.JsonSlurper
def call(total_record, url, bearer_token, scriptId, payload) {
Integer tot = total_record as Integer // Convert string to Integer
HttpURLConnection connection = null;
if (tot == 1) {
StringBuilder stringBuilder = new StringBuilder("${url}");
stringBuilder.append(URLEncoder.encode("${scriptId}", "UTF-8"))
URL put_url = new URL(stringBuilder.toString());
connection = (HttpURLConnection) put_url.openConnection()
connection.setRequestMethod("PUT");
println("Executing PUT")
}
else if (tot == 0) {
URL post_url = new URL("${url}");
connection = (HttpURLConnection) post_url.openConnection()
connection.setRequestMethod("POST");
println("Executing POST")
}
else {
println("Total records can not be calculated")
}
connection.setRequestProperty("Authorization", bearer_token);
connection.setRequestProperty("Content-Type", "application/json;odata=verbose")
connection.setRequestProperty("Accept", "application/json")
connection.setDoOutput(true)
OutputStream pos = connection.getOutputStream()
pos.write(payload.getBytes())
pos.flush();
pos.close();
println(connection.responseCode)
def msg = null
if(connection.responseCode == 200){
msg = "Executing Deployment"
}
else{
msg = "Error is Connection\n"
msg += connection.responseCode
}
connection.disconnect()
return msg
}
讀取腳本/文件內容作為輸入
File f = new File("out.py")
def content = f.readLines().toString()
有效載荷示例:
String payload = """{
"Definition": {
"name": "filename",
"modifiedBy": "user_name",
"active": true,
"systemId": "filename_sid",
"script": "${content}"
}
}"""
我的腳本文件包含:
import clr
import sys
import System
# Testing Changes Priyabrata
clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)
clr.AddReference('System.Xml')
from System import DateTime, Random
Offeringid = ""
tableInfo = SqlHelper.GetTable("COMPOSIOTION")
以下是錯誤響應:
{"error":{"code":"110000","message":"One or more fields in the API request contains invalid data. See the log for more information","target":"/api/script/v1/globalscripts/37","details":[{"code":"110000","message":"ScriptDefinition.Script: After parsing a value an unexpected character was encountered: S. Path 'ScriptDefinition.Script', line 13, position 18."}],"internalMessage":"Model validation failed"}}
但是,當我嘗試在 Python 中編碼時,腳本內容也使用 python 請求模塊發布到端點。
但這會產生錯誤,即 groovy 中的 400 Bad Request。 我要求按照組織標准在 groovy 中制作管道。
在此處輸入圖像描述任何建議將不勝感激。 謝謝。
你的郵政編碼很好。
問題不在於內容大小,而在於您正在構建 json 有效負載。
你的content
有雙引號
因此,使用 json 構建的字符串插值如下:
def content = 'println "world"'
def payload = """{ "script":"${content}" }"""
會給你不正確的 json:
{ "content": "println "world"" }
最好使用groovy.json.JsonBuilder
或groovy.json.JsonOutput
def content = 'println "world"'
def payload = new groovy.json.JsonBuilder([
'active': true,
'script': content
]).toPrettyString()
這將導致格式良好的 json 帶有所有必需的轉義雙引號和其他特殊字符
{
"active": true,
"script": "println \"world\""
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.