简体   繁体   English

如何在 Next.js 重写中传递正文参数?

[英]How can I pass body params in a Next.js rewrite?

I have this POST request:我有这个POST请求:

const requestOptions = {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      body: JSON.stringify({ 
          grant_type: "password",
          client_id: "client",
          client_secret: "clientsecret",
          username: "matias@gmail.com",
          password: "12341234" 
      })
    };

    const token = fetch("http://localhost:3000/connect/token", requestOptions)
                    .then(response => console.log(response))
                    .catch(error => console.log(error));

I would like to rewrite it so I can bypass CORS error in development environment.我想重写它,这样我就可以绕过开发环境中的 CORS 错误。 To achieve this, I have this code in my next.config.js file:为此,我在next.config.js文件中有以下代码:

const nextConfig = {
  reactStrictMode: false,
  async rewrites() {
    return process.env.NODE_ENV === "development"
      ? [
          {
            source: "/connect/token",
            destination: "http://localhost:4446/connect/token",
          }
        ]
      : [];
  },
}

How can I specify the body params ( grant_type , client_id , client_secret , username and password ) to this Next.js rewrite?如何为 Next.js 重写指定正文参数( grant_typeclient_idclient_secretusernamepassword )? The error I am getting suggest these values are not present in my rewrite:我得到的错误表明这些值在我的重写中不存在:

fail: IdentityServer4.Validation.ClientSecretValidator[0]
      No client identifier found

I don't know about Next.js, but have you tried to add the url to the client configuration as Allowed Cors Origins ?我不了解 Next.js,但您是否尝试将 url 作为Allowed Cors Origins添加到客户端配置?

You could add in your develop client the url http://localhost:3000 .您可以在您的开发客户端中添加 url http://localhost:3000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM