Unable to upload file using REST API


#1

Hello,
I am trying to upload a csv file using REST API and Object Builder. Below is a snippet of my code (I am running it locally on my MacOS, filename is also called csv). Looking at the console, the request it is receiving is not formatted correctly.
Expected:

=> {“csv”=>

{“filename”=>“csv”,

“type”=>“application/octet-stream”,

“name”=>“csv”,

“tempfile”=>#<File:/tmp/RackMultipart20180919-23531-199aa4a>,

“head”=>“Content-Disposition: form-data; name=\“csv\”; filename=\“csv\”\r\nContent-Type: application/octet-stream\r\n”},

“delete_mode”=>false}
Actual: {“csv”=>“csv”, “delete_mode”=>false}

Any help would be appreciated…Thanks!

def builder = new RestRequestObjectBuilder()

def requestObject = builder.withRestRequestMethod(‘POST’)

.withRestUrl(GlobalVariable.SUT + “/api/preprocess_csv”)

.withHttpHeaders([

new TestObjectProperty(‘Content-Type’, ConditionType.EQUALS, ‘multipart/form-data’),

new TestObjectProperty(‘Cookie’, ConditionType.EQUALS, anpkSess.toString()),

new TestObjectProperty(‘Authorization’, ConditionType.EQUALS, ‘####’)])

.withMultipartFormDataBodyContent([

new FormDataBodyParameter(“csv”, “csv”, “PARAM_TYPE_FILE”),

    new FormDataBodyParameter("csv", 'Content-Disposition: form-data; name="csv"; filename="csv"', "PARAM\_TYPE\_TEXT")



    \]).build()

Resp = WS.sendRequest(requestObject)

print(Resp.getStatusCode())

print(Resp.getResponseText())

Thanks, Nanda


#2

Hello Experts,
Can you shed some light on this? I have tried various combinations but it still keeps returning an error saying that the field is invalid. If you could share some code which is working, it would be much appreciated. I really need help on this one as it is blocking automation of a majority of my test cases!
Best,
Nanda


#3

Hi @Swaminathan_Nandakum

We’re also having this problem, our workaround is to use cURL via ProcessBuilder instead.

ProcessBuilder pb = new ProcessBuilder(“curl”, “-X”, “POST”,
““https://samplesite/index.php?/api/v2/add_attachment_to_result/3996=””,
“-H”, ““Authorization: Basic tHiSiSaToKeN””
, “-H”, ““Content-Type: multipart/form-data””, “-H”, ““cache-control: no-cache””,
“-F”, ““attachment=@C:\test-image.jpg””)
pb.redirectErrorStream(true)
Process p2 = pb.start();