Java Object to Multipart

1,213 views
Skip to first unread message

Artur Sentsov

unread,
Nov 21, 2014, 5:12:38 PM11/21/14
to rest-a...@googlegroups.com
Hi All,

I am able to pass Java Object into body() and it will automatically convert an object into a Json string. Is there a way to convert Java Object into multipart request?

Let me know.

Thanks

Artur

Johan Haleby

unread,
Nov 22, 2014, 1:15:32 AM11/22/14
to rest-a...@googlegroups.com
Sure, pass it into multipart(..).


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Artur Sentsov

unread,
Dec 9, 2014, 7:41:01 PM12/9/14
to rest-a...@googlegroups.com
Here is how my working test looks like now:
given()
    .headers(myHeaders)
    .multiPart("userID", "2345234")
    .multiPart("description", "this is general description")
    .multiPart("address", "address here")
    .multiPart("city", "city here")
    .multiPart("firstName", "first name")
    .multiPart("lastName", "last name")
    .multiPart("phone", "phone")
    .multiPart("fax", "fax")
    .multiPart("doctorName", "dname")
    .multiPart("doctorState", "dstate")
    .multiPart("doctorZip", "dzip")
    .multiPart("type", "some type")
    .multiPart("expirationDate", "10/10/2010")
    .multiPart("measurment1", "12.1")
    .multiPart("measurment2", "33.0")
    .multiPart("measurment3", "26")
    .multiPart("measurment4", "52")
    .multiPart("measurment5", "54")
    .multiPart("measurment6", "99")
    .multiPart("measurment7", "432")
    .multiPart("measurment8", "3"
    .multiPart("measurment9", "32.2")
    .multiPart("measurment10", "22.3")
    .multiPart("measurment11", "1.0")
    .multiPart("measurment12", "22")
    .multiPart("measurment13", "77")
    .multiPart("measurment14", "44")
    .multiPart("measurment15", "55")
    .multiPart("measurment16", "66")
    .multiPart("measurment17", "55")
    .multiPart("measurment18", "44")
    .log().everything()
.expect()
    .statusCode(201)
    .contentType(ContentType.JSON)
    .log().everything()
.when()
    .post("myURL");

Instead of doing all of those multiparts, i wanted to pass the object i have, where all of those variables are set already, and that object would get converted into multipart automatically.

Artur Sentsov

unread,
Dec 9, 2014, 7:56:15 PM12/9/14
to rest-a...@googlegroups.com
I want java object to convert into multipart when variable name in that object will become a content name and variable value in that object become content for that content name.

So after conversion rest assured will post to api something like following:
controlName=userID, mimeType=text/plain, charset=<none>, fileName=<none>, content=2345234
controlName=description, mimeType=text/plain, charset=<none>, fileName=<none>, content=this is general description
controlName=address, mimeType=text/plain, charset=<none>, fileName=<none>, content=some address
controlName=city, mimeType=text/plain, charset=<none>, fileName=<none>, content=somecity
controlName=firstName, mimeType=text/plain, charset=<none>, fileName=<none>, content=Peter
controlName=lastName, mimeType=text/plain, charset=<none>, fileName=<none>, content=White
controlName=phone, mimeType=text/plain, charset=<none>, fileName=<none>, content=9161112222


On Friday, November 21, 2014 2:12:38 PM UTC-8, Artur Sentsov wrote:

Johan Haleby

unread,
Dec 10, 2014, 2:42:09 AM12/10/14
to rest-a...@googlegroups.com
If the object that you want to serialize to JSON is called "object" then you should be able to do like this:

given().multiPart("controlName", object, "application/json"). ..


Regards,
/Johan

--

Artur Sentsov

unread,
Dec 10, 2014, 1:24:45 PM12/10/14
to rest-a...@googlegroups.com
When i do that i get:
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)

Johan Haleby

unread,
Dec 10, 2014, 1:54:35 PM12/10/14
to rest-a...@googlegroups.com
Could you show use the java object you try to serialize?

Artur Sentsov

unread,
Dec 10, 2014, 1:57:44 PM12/10/14
to rest-a...@googlegroups.com
It is just an object with setter and getters.

RX rx = new RX();
rx.setUserId("123456");
rx.setDescription("rx Test");
rx.setAddress("345 H Street");
rx.setCity("Rancho Cordova");
rx.setFirstName("Peter");
rx.setLastName("White");
rx.setPhone("9167871212");

Artur Sentsov

unread,
Dec 10, 2014, 2:03:55 PM12/10/14
to rest-a...@googlegroups.com
I was able to fix that JsonMappingException

Now, when i do
multiPart("controlName", rx, "application/json")

Java object converts and I can post it to api, and in the log i see:
controlName=myControlName, mimeType=application/json, charset=<none>, fileName=<none>, content={"UserId":"123456","Description":"rx Test","Address":"123 H Street","City":"Rancho Cordova","FirstName":"peter","LastName":"White","Phone":"9167871212", "Address":"address", "city":"city"}

But what i am trying to achieve is that my java object convert to the following:       
controlName=userID, mimeType=text/plain, charset=<none>, fileName=<none>, content=2345234
controlName=description, mimeType=text/plain, charset=<none>, fileName=<none>, content=this is general description
controlName=address, mimeType=text/plain, charset=<none>, fileName=<none>, content=some address
controlName=city, mimeType=text/plain, charset=<none>, fileName=<none>, content=somecity
controlName=firstName, mimeType=text/plain, charset=<none>, fileName=<none>, content=Peter
controlName=lastName, mimeType=text/plain, charset=<none>, fileName=<none>, content=White
controlName=phone, mimeType=text/plain, charset=<none>, fileName=<none>, content=9161112222


Johan Haleby

unread,
Dec 10, 2014, 2:09:05 PM12/10/14
to rest-a...@googlegroups.com
Oh I see, you want to serialize each field to it's own control name!? REST Assured won't help you in that case. I suppose you could write your own filter that does this though. But why would you want to do this? Why not just send everything as form parameters or JSON?

Artur Sentsov

unread,
Dec 10, 2014, 3:33:53 PM12/10/14
to rest-a...@googlegroups.com
Yes, i want to serialize each field to it's own control name.
That is how the API i am currently testing works. There were some reason why developers made it like that, i just don't know those reasons. To me it would be much easier to test it using JSON.

I will try to figure out what i can do.
Reply all
Reply to author
Forward
0 new messages