Issue
I have the below code for doing a POST request to a REST API end point to update SAP application data.
#!/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
}
Reading the script/file content as input
File f = new File("out.py")
def content = f.readLines().toString()
Example Payload:
String payload = """{
"Definition": {
"name": "filename",
"modifiedBy": "user_name",
"active": true,
"systemId": "filename_sid",
"script": "${content}"
}
}"""
Basically , I want to upload some python script content (python code) to API end point url.
The above code is working good when I simply post any random text which is not code.
But it gives 400 Bad Request when it reads the file content as code.Example error below:
my script file contains:
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")
Below is error response:
{"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"}}
However when i try to code in Python, script content also posted to endpoint using python requests module.
But this gives error i.e. 400 Bad Request in groovy. I have a requirement to make the pipeline in groovy as per organization standard.
enter image description here Any suggestion would be much appreciated. Thanks.
Solution
your POST code is fine.
problem not in content size , but in a way you are building json payload.
you have doublequotes in content
so, using string interpolation for json building like this:
def content = 'println "world"'
def payload = """{ "script":"${content}" }"""
will give you incorrect json:
{ "content": "println "world"" }
better to use groovy.json.JsonBuilder
or groovy.json.JsonOutput
def content = 'println "world"'
def payload = new groovy.json.JsonBuilder([
'active': true,
'script': content
]).toPrettyString()
this will result well-formatted json with all required escaped doublequotes and other special characters
{
"active": true,
"script": "println \"world\""
}
Answered By - daggett