How to send array as parameter in API Call? - java

How to send array as parameter in API Call?
I just want to know how can I pass and array as parameter in Java,
Like:-
"list":[
{
"name":{{$randomFullName}},
"account":{{$randomAlphaNumeric}},
"accountbalance":{{$$randomInt}}
}
"Message": "Dear {{name}}, your {{$accountnumber}} has been credited with Rs.{{($accountbalance)}}.
}
I just want to pass name, account, account-balance in the message body.

Your question seems to be a bit unclear.
If you just want to set the values during runtime, before sending the request, you need to set the environment and add these as the variables in the postman.
Or if you're trying to build the whole list dynamically, then you can write the complete code for that in the request script and simply use that while sending the request. This can help you with the same.

it doesn't work that way . You can't use a parameter as an object in a request body . Instead , you should extract the list in your code and then send message accordingly in code only .

Related

Android Retrofit - send dynamic number of POST parameters

I need to send a dynamic number of POST parameter values to an endpoint (there could be 1 or there could be 50). All of them will have the same key value.
Is this possible? I can't seem to figure out how to create a RequestBody that encompasses something like this, even when I try to construct it in plain text.
I have the list of strings prepared for it, but I just don't know how to create this kind of thing. The endpoint works in PostMan when I input a lot of post form parameters with the same key value, so the endpoint is setup properly for it. I'm just not sure if Retrofit supports this kind of thing, and I cannot seem to find any info around it.
I'm currently working with Java instead of Kotlin. Thoughts?
You can also pass multiple field parameter to your request like this:
#FormUrlEncoded
#POST("/oauth/access_token")
Call<YourResponseObject> sendData(
#FieldMap Map<String, String> params
);
The map can take variable number of args.
So you can pass data like:
/*
map = { "field1_key"="value1", "field2_key"="value2", "field3_key"="value3", ...}
*/
retrofit().create(YourInterface.class).sendData(mapOfFields)
p.s: retrofit() is a method that returns a Retrofit instance to work with.

Java Jersey-client: witing test a GET method

I want to write a unit test for GET method.I have a GET request that returns a json file of when I request path localhost:9090/application/a/b:
{
name: a,
Age:b
}
I want to write unit test using jersey client. I tried writing as such:
Response response = target("a/b").request(MediaType.APPLICATION_JSON).get();
The objective is to retrieve from the GET response and assert the returned json with an expected value:
assertEqual(expected value:{name: a, Age:b},returned json from response)
However, I am not quite sure how I can write this. Can someone guide me on this? I have been searching through many code samples but mostly it is for POST request so I am not sure how this is implemented for GET request.
Edit: I am not required to post anything (I can just grab most code sample for POST) What I am trying to do is just to invoke a call using the path without sending any json document or object and have it returned to me a response in json. After that, I am supposed to grab this json response, and then assert the object with an expected document. The part that I need guidance on is writing the right Response line of code. Also, since my test class has to inherit a jersey client parent, I am expected to use the Response class. I couldnt find any example like this online. That is why I am here to ask.
Thanks in advance

Creating a filter to check a case for a letter in a field in the json body in REST assured

I have a request in the form of json,which looks like this.
{"User":{"email":"test#test.com","FName":"fname"}}
When I try to send it via REST assured ,the U in the User is seen to change its case.i.e. changes to a lower case.
To send the request I have created my own serialized classes. The end-point is seen like this:
{"user":{"email":"test#test.com","FName":"fname"}}
but somehow it is not changing the case of the remaining fields.I don't knwo why this is happening.
I've even tried to create a filter for a request specification,but couldn't go any further with that too. I also then thought of first converting the serialized object to a gson,and then check the case of the User, still no luck.
Error I get is:
The class, User,does not match the payload object for payload.
Please note I am trying to use the service of another team,so I really don't have an access to their code-base(Although not needed).Observe the space between the first , and user in the above message, is it worth noting?
I finally got away with it by converting the object(JSON) into a JSON string/payload.
And while passing it as a form parameter,passed the string/payload.
Somehow,still couldn't figure out why the formparameter/formparam option in RESTAssured did not allow the serialized object to go through. But,anyway got around it this time.
Thanks for the suggestions all.

Sending custom object as a parameter for a web-service

How can one specify a custom object as a parameter for the web-service's method when invoking through SOAP message?
Say I have this code:
SOAPElement operation = body.addChildElement("MyMethod", "", trgNamespace);
SOAPElement value = operation.addChildElement("arg0");
value.addTextNode("i need to send here a custom object not a string")
request.saveChanges();
The addTextNode sends a string whereas I need to send my own object as a parameter for invocation.
You have to serialize your object to transfer it over the line. Serialization is often done using XML or JSON, see the following link for details: http://en.wikipedia.org/wiki/Serialization
That should get you on the right path.
Maybe try higher level and use wsdl-based stubs generator for java? It's Axis wsdl to java
I could think of another approach
You can send that custom object as a binary data (I assume your object is serialize-able). Then encode that data in say Base64 encoding.
There is similar problem asked earlier. Plz check out this link. This seems most relevant to your problem.
Another link mentioned in the above posting gives nice overview of handling these type of problems in general.

Is it possible to send an arraylist as a response to jquery?

Is it possible to send an arraylist as a response(from action class) to jquery?If so please give sample code
yes it is possible to send send an arraylist as a response(from action class) to jquery.
jQuery.getJSON() method can do this easily.
http://api.jquery.com/jQuery.getJSON/
and For details on the JSON format, see http://json.org/.

Categories

Resources