Next 13 Route Handler在从请求体获取数据时出现问题(app/api/auth)。

huangapple go评论72阅读模式
英文:

Next 13 Route Handler having issues getting data from body (app/api/auth)

问题

我尝试从新的 export async function POST( req: Request) 中获取请求体,但我得到的是流而不是内容类型。
这是位于 api/auth/signup 中的路由处理程序的示例代码:

export async function POST( req: NextRequest ) {
    // 发送成功响应
    console.log( req.body );
    return new Response( req.body, {
        status: 200,
    });
}

我正在发送请求的处理程序如下:

function handleSubmit( event: FormEvent<HTMLFormElement> ) {
    event.preventDefault();
    // 表单验证代码在这里
    console.log( "Sent the request to the server" );
    fetch( "http://localhost:3000/api/auth/signup", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
            username: username,
            password: password,
            email: email,
        }),
        next: { revalidate: 0 },
    })
    .then( res => res.json())
    .then( data => console.log( data.username, data.password, data.email ));
}

这个处理程序的结尾可以打印出我发送到服务器的值,但尝试在服务器的 req.body 上访问它们会得到一个可读流,显然没有相关属性。
这是从请求主体获取的可读流的示例图像:可读流图像

应用程序文件夹是实验性的,但很难看到一个简单的功能不起作用。感觉像是我的问题(99.9% 的编程问题:D)

这一切都通过 http / localhost 运行,这可能会引发问题。

我已经尝试寻找解决方案,但似乎只有其他人遇到了相同的问题。

英文:

When I try to get the body out of the new export async function POST( req: Request) I am getting it as a stream instead of the content type.
Here is the route handler located in api/auth/signup
Folder layout image

export async function POST( req: NextRequest ) {
	//Send success response
	console.log( req.body );
	return new Response( req.body, {
		status: 200,
	});
}

The handler im sending it from is here

function handleSubmit( event: FormEvent&lt;HTMLFormElement&gt; ) {
    event.preventDefault();
    //form validation code here
    console.log( &quot;Sent the request to the server&quot; );
    fetch( &quot;http://localhost:3000/api/auth/signup&quot;, {
       method : &quot;POST&quot;,
       headers:{
           &quot;Content-Type&quot;: &quot;application/json&quot;,
       },
       body: JSON.stringify({
           username: username,
           password: password,
           email   : email,
       }),
       next: { revalidate: 0 },
    })
    .then( res =&gt; res.json())
    .then( data =&gt; console.log( data.username, data.password, data.email ));
    
}

The end of this handler is able to print out the values ive sent to server, but trying to access them on the server's req.body is giving me a readable stream which obviously doesnt have the properties on it.
Readable stream from body image

The app folder is experimental, but having a hard time seeing a simple feature just not work. Feel like this is a me problem (99.9% of programming :D)

This is all running through http / localhost if that could cause issues.

Ive tried looking for solutions but it seems like there are only other people having the same issue.

答案1

得分: 2

需要将请求解析为JSON格式。

export async function POST(request: Request) {
  const { user_id } = await request.json()
  console.log(user_id)

  return new Response('Success');
}
英文:

You need to parse the request into json format

export async function POST(request: Request) {
  const { user_id } = await request.json()
  console.log(user_id)

  return new Response(&#39;Success&#39;);
}

huangapple
  • 本文由 发表于 2023年3月4日 06:58:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定