0

I am trying to make a post request and getting this exception:

"unsupported BodyInit type" 

I think the issue is with the body of the request. phoneNumbers takes on the form phoneNumbers = ["1234567890", "1234567891"] (i.e. a list of strings). I tried to do JSON.stringify(phoneNumbers) as the body, but that seems to return "[]", even if the list is not empty.

 export async function findUsersByPhoneNumbersNotFollowing(userId, phoneNumbers) { const reqConfig = { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json', }, body: phoneNumbers, }; const response = await authfetch(`${API_URL}/following/recommendations/${userId}`, reqConfig); if (response.error) { throw new Error(response.error); } return response; } 

Where am I going wrong? The API endpoint is expecting List<String> (using spring framework, and the controller method takes this param in annotated @RequestBody)

1 Answer 1

1

Try sending a JSON Object instead a plain array:

 const reqConfig = { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json', }, body: JSON.stringify({ paramName: phoneNumbers }), }; 

Replace paramName for the name you are expecting on your API endpoint.

Sign up to request clarification or add additional context in comments.

2 Comments

Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token
Can you share the code (or ate least part of it) you are using in the endpoint?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.