admin 管理员组文章数量: 1086019
I'm facing an issue in my Next.js application where I'm passing a decimal number (as a string) as a query parameter to my API. The API works fine when tested directly using Swagger and also works when I send the request without using server actions.
However, when using server actions, the decimal number seems to be sent as an integer (for example, 2.5
becomes 25
), and this causes the request to fail.
Here's how I'm calling the server action:
"use server";
async function fetchData() {
try {
const response = await fetch(`${process.env.API_URL}/endpoint?amount=2.5`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`,
},
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error("Error:", error);
}
}
Issue:
When I pass the amount as a decimal number (e.g., 2.5), my API receives 25 instead.
This only happens when using the server action to make the request.
The same request works perfectly fine when I send it directly
(without using server actions) like so:async function fetchData() { try { //using NEXT_PUBLIC_API_URL for browser visibility const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/endpoint?amount=2.5`, { method: "GET", headers: { "Content-Type": "application/json", Authorization: `Bearer ${process.env.NEXT_PUBLIC_ACCESS_TOKEN}`, //using NEXT_PUBLIC_ACCESS_TOKEN for browser visibility }, }); if (!response.ok) { throw new Error("Network response was not ok"); } const data = await response.json(); console.log(data); } catch (error) { console.error("Error:", error); } }
Both the NEXT_PUBLIC_API_URL and API_URL are the same, and the tokens (NEXT_PUBLIC_ACCESS_TOKEN and ACCESS_TOKEN) are also the same.
What could be causing the decimal value to be transformed incorrectly when passed through a server action? How can I ensure the decimal value is sent correctly in the query parameter when using server actions?
versions that I am using "next": "14.2.3", "react": "18.2.0",
EDIT: Answer - I was missing 'Accept-Language' header and without this API was not behaving correctly when using server cation
本文标签: Nextjs server action not receiving correctly decimal numbersStack Overflow
版权声明:本文标题:Next.js server action not receiving correctly decimal numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744093289a2532434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论