How to post multiple values at same key using Retrofit 2? - java

I am stuck on this part which requires me to post multiple values to the server. I am using Retrofit.
The whole scenario is like this: there is a form where user can fill in the details like his name, age, address and can even select multiple values from certain questions. Particularly, there are 5 types of questions: Int, String, Single Choice, Boolean and Multiple Choice. As you can see I can have Int, String, Boolean and Array types.
So I created FieldMap as > to save all values in it and then posting it. I am able to post all values except Array which my server is expecting as query dict i.e. if I have FieldMap as "143": ["hello", "hey", "hi"]; server is expecting me to send it as 143=hello&143=hey&143=hi.
It is expecting me to send all array values to same key as shown above.
Can somebody help me with this? How can I achieve this using Retrofit 2?

Have you tried sth like following?
#GET("endpoint")
fun getSomething(#Query("143") items: List<String>): Call<Response>

Related

Java serialization of a query to list of strings

I stumbled upon a strange issue and cant find quick solution to deal with it. Front side of my application sends query to back-end I added square brackets for readability.
as it should be
http://localhost:3000/?params=[First_value],[Anhui Jantene Hone Textiles Co., Ltd.],[Third_Value]
as it is
http://localhost:3000/?params=[first_value],[Anhui Jantene Hone Textiles Co.], [Ltd.],[Third_Value]
params are serialized to List but there is a problem when given value on a front-side has comma in the name, then instead of one value I get 2 entries. In a given example expected size of a list should be 3 but I get 4 elements.
Any help is appreciated
You can't do this in the query string because of how a comma is treated. You need to use a POST with a request body like this:
["First_value", "Anhui Jantene Hone Textiles Co., Ltd.","Third_Value"]

Its possible send array in url

I would like to know if i can send an array of this type endate [] in the parameter fecha2 of the following url:
http://www.xxxx.com/app/disponibilidad?idUser&fecha2&fecha1&desde&hasta
endate[] has several date I want to send in a single url. I would like to know if that is possible
You can get a good idea in How to pass an array within a query string?
You can also encode it to JSON and pass it as a single string, then decode it on your backend. Sounds simpler to me.

How to store jmeter response to an array in order to make request with the given array?

I want to create a Jmeter testplan and I need a object that is create with another object reference. I want to create this object 'owner' 100 times and with the response create 100 'devices' in which one of the fields will be an 'owner_ref'.
I want to create the devices with the corresponding ids like
ownerId1-->device1
ownerId2-->device2 and so on.
Im using now:
testPlan:
HttpRequest
Json Path Postprocessor--> extract 'id' to variable 'ownerId'
How I can create an array of ownerIds and iterate through this 100 to create new request with these ids?
I am thinking in use beanShell but should be another easier and cleaner method to do.
Thanks to all!
As JSON Path PostProcessor documentation states, you can use Match Numbers parameter:
-1 means extract all results, they will be named as variable name_N (where N goes from 1 to Number of results)
So you could configure your JSON Path PostProcessor like this:
Variable Names: ownerId
...
Match Numbers: -1
Which will produce variables: ownerId1,...,ownerIdN
And then you have a few options, most obvious of them is to use ForEach Controller exactly as help describes
ForEach Controller
Input variable prefix: ownerId
Output variable: currentId
So now any sampler under that controller can use ${currentId}

rest-assured: Format of number change after being retrieved using JsonPath

I am observing that the format or value of an item from a rest-assured response is changed when I try to retrieve the item using JsonPath.
Value of the field is 16637906.26 when I get the response.asString().
However when I retrieve this specific information using JsonPath, it returns the number as 1.6637906E7.
Below is the rest-assured JsonPath I used to get the data:
List<Map<String,?>> values = JsonPath.with(actResponse).param(param,value).get("entries.findAll { entries -> entries."+param+" == "+param+" }")
where param is a variable pointing to parameter I use.When I print this List, I can see that the value is then displayed as 1.6637906E7. All other smaller values are returned properly.
I tried to play with it a bit like converting the number etc, however nothing seem to work.
Highly appreciate if someone can guide me through this.

User Defined Index for an ArrayList of IDs

I'm in a situation where (in Java) I need to define an arraylist of generated ids. I don't know how many would be generated at any given time, but I do know that when one is generated, the user who generated it would need to set a custom index, and be able to retrieve it by that index. What would be the generally accepted standard way of storing and working with a data structure like this? An arraylist of arrays?
Sounds like a use case for a Map which you can use the ID as the key and a value (or potentially an array of values, if multiple values can have the same id) as the value. You can then index into the map and retrieve data using the key. The benefit is that this works even if you want to change the ID from an int to a String or even some other idea.
The problem with using a List like this is if I have two ids 1 and 3000 then there are 2998 indices that are wasted, which is not exactly ideal.

Categories