Guide to sending an Array of objects in the request body using RestAssured in Java

Here is the Request I am working with:

[
  {
    "userId": "value1"
    
 },{
    "userId": "value2"
    
  }
]

I attempted to create a POJO class and construct the request, as well as using an ArrayList, but there seems to be an issue with the request body.

Answer №1

If you're struggling with doing List<User>.class, consider using ParameterizedTypeReference as an alternative

Answer №2

Discovered solution for the following request,

API call details,

[
  {
    "userId": "valueA"
 },{
    "userId": "valueB"
  }
]

Resolution :

public String makeAPICall(String id1, String id2){

   JSONObject userA = new JSONObject();
   userA.put("userId", id1);

   JSONObject userB = new JSONObject();
   userB.put("userId", id2);

   JSONArray jsonArray = new JSONArray();
   jsonArray.put(userA);
   jsonArray.put(userB);

   String jsonStr = jsonArray.toString();

   System.out.println("jsonString: "+jsonStr);
   return jsonStr;
}

Result:

[{"userId":"dgsf12"},{"userId":"lksk25l"}]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The NetSuite https.post() method is throwing an error that reads "Encountered unexpected character while parsing value: S. Path '', line 0, position 0"

I'm currently facing an issue when trying to send the JSON data request below to a 3rd party system using the "N/https" modules https.post() method. Upon sending the request, I receive a Response Code of "200" along with the Error Message "Unexpected ...

Converting Java byte[] arrays into ArrayList using Gson

Using Gson for object serialization and deserialization can be tricky, as Gson tends to convert byte[] arrays into ArrayLists. Here is an example of an Object class: public class RequesterArgument implements Serializable { private Object data; pu ...

Showing all columns in a table using Flutter

I'm currently developing a test application using Dart, which is designed to display and insert data into a database hosted on 000webhost.com. In my app, I'm trying to display JSON data in a table where all the columns are contained within a sin ...

Tips for modifying JSON property names during the parsing process

As outlined in the JSON.parse documentation, a reviver function can be utilized to modify the value of each property within the JSON data. Here is an example: JSON.parse('{"FirstNum": 1, "SecondNum": 2, "ThirdNum": 3}', function(k, v) { return ...

Use the JavaScript .replaceAll() method to replace the character """ with the characters """

My goal is to pass a JSON string from Javascript to a C# .exe as a command line argument using node.js child-process. The JSON I am working with looks like this: string jsonString = '{"name":"Tim"}' The challenge lies in preserving the double q ...

Encountering a 403 Forbidden error in Spring Boot while attempting to upload an image to the server via a POST request

Currently, I am facing an issue while trying to upload photos into the database as I keep encountering error 403. Interestingly, everything was functioning smoothly when I tested the endpoint using POSTMAN. However, upon creating the axios request, the err ...

Express.js response.json method can be used to render both HTML and JSON content in the

I have a router that is sending JSON data to the client as shown below: router.get('/', function(req, res, next) { UserModel.selectAllUsers(function(err, vals){ if(err){ console.log(err); } res.json({d: v ...

I'm not sure how I can retrieve the pollId from a ReactJS poll

In my React code, I am fetching a poll using an API. However, I am facing an issue while working on the handleChange function for making a POST API request. The information required for the request includes PollId, userId, and answer. I am able to retrieve ...

`What is the process for converting a CURL command with a token into Swift?`

Having received a CURL command from our custom REST API without any accompanying documentation, I am now faced with the task of translating and implementing it in Swift. In my previous experimentation with other APIs, I have used SwiftyJSON. However, I am ...

Tips for verifying and controlling dropdown values using WebDriver?

Is there a way to check the status of percentage values in a drop-down, where some are enabled and others disabled? I am looking for a method to obtain the XPath of a specific value and determine if it is enabled or disabled. Click on the image below for ...

A guide to implementing polymorphic serialization without the use of annotations or mixins

In the city of Jackson, we have the option to utilize certain annotations: @JsonTypeInfo @JsonSubTypes @JsonSubTypes.Type These can be used for polymorphic serialization. We have two choices: Applying these annotations directly on the data model, which ...

What is the best way to combine data from two rows into one row?

Can someone help me with merging 2 sets of Line data into a single row for each entry? For example, the 'Green Bay Packers @ Chicago Bears' row should display '+3.5000 and -3.5000', while 'Atlanta @ Minnesota' should show &apo ...

Creating a PHP JSON response that incorporates an HTML layout is a dynamic

I'm having some trouble with a certain issue: I am attempting to develop a jQuery/AJAX/PHP live search bar. Although I am successfully calling search.php, the response displayed in the console always includes the contents of my master.php file (which ...

json is failing to retrieve the correct information from the database array

I ran a query and received two rows of data, after testing it in phpadmin. However, when I checked Firebug, I could only view the information from one row. What might be causing this discrepancy? $data = mysql_fetch_assoc($r); } } head ...

Discovering a web element: A guide

Couldn't locate a specific element on the webpage https://cloud.google.com/products/calculator/ Trying to find and interact with the 'Number of GPUs' element on the aforementioned site, then select '1' from the dropdown menu that ...

What steps should be taken to integrate a RESTful API into a React application?

I'm new to both React and microservices. I'm currently working on developing a UI application using React, which is intended to connect to a database in the future. However, at present, the app only serves as the frontend of the system. In order ...

How can I unravel a JSON array of tuples in Jq to create numerous individual elements?

Looking for a way to parse this complex json data: {"op":"mcm","clk":"1147179697","pt":1439869512969, "mc":[ {"id":"1.120040663", "rc":[ {"atb":[[7.6,35],[7.2,25]],"id":11111}, {"atb":[[1.04,100],[1.02,200 ...

Get the file from the web browser

Hey there, greetings from my part of the world. I have some straightforward questions that I'm not sure have simple answers. Currently, I am working on a web application using the JSP/Servlet framework. In this app, users can download a flat text fil ...

Should PageFactory be utilized with Selenium?

I'm struggling to make a decision about whether or not to utilize PageFactory in my Selenium PageObjects. From what I can gather, the main benefit of using it is null safety, ensuring that WebElements are not initialized until they are actually neede ...

The system does not acknowledge "ENVIRONMENT" as a command that can be executed either internally or externally, or as a batch file that can be

While running my Next.js application, I encountered the following error in a script: "scripts": { "dev: "ENVIRONMENT=env/.env.development next dev", "check": "npm run format && npm run eslint", "e ...