我正在尝试发出发布请求并遇到此异常:
"unsupported BodyInit type"
我认为问题在于请求的主体。 phoneNumbers
的格式为phoneNumbers = ["1234567890", "1234567891"]
(即字符串列表)。我尝试将JSON.stringify(phoneNumbers)用作正文,但是即使列表不为空,也似乎返回“ []”。
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;
}
我要去哪里错了? API端点期望List<String>
(使用spring框架,并且控制器方法在带注释的@RequestBody
中采用此参数)
尝试发送JSON对象而不是普通数组:
const reqConfig = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({
paramName: phoneNumbers
}),
};
将paramName
替换为您在API端点上期望的名称。