Javascript SyntaxError:JSON中位置1处的意外标记u curl请求

Javascript SyntaxError:JSON中位置1处的意外标记u curl请求,javascript,json,curl,Javascript,Json,Curl,有人知道我在powershell上运行此请求时出现此错误的原因吗 curl.exe-d'{“username”:“username”}'-H“内容类型:application/json”-X POSThttp://localhost:5200/auth index.js文件: const dialogflow = require("dialogflow"); const uuid = require("uuid"); const express = req

有人知道我在powershell上运行此请求时出现此错误的原因吗

curl.exe-d'{“username”:“username”}'-H“内容类型:application/json”-X POSThttp://localhost:5200/auth

index.js文件:

const dialogflow = require("dialogflow");
const uuid = require("uuid");
const express = require("express");
const StreamChat = require("stream-chat").StreamChat;
const cors = require("cors");
const dotenv = require("dotenv");

const port = process.env.PORT || 5200;

async function runSample(text, projectId = process.env.GOOGLE_PROJECT_ID) {
  const sessionId = uuid.v4();

  const sessionClient = new dialogflow.SessionsClient();
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        text: text,
        languageCode: "en-US",
      },
    },
  };

  const responses = await sessionClient.detectIntent(request);

  const result = responses[0].queryResult;
  if (result.action === "input.unknown") {
    // If unknown, return the original text
    return text;
  }

  return result.fulfillmentText;
}

dotenv.config();

const app = express();
app.use(express.json());
app.use(cors());

const client = new StreamChat(process.env.API_KEY, process.env.API_SECRET);

const channel = client.channel("messaging", "dialogflow", {
  name: "Dialogflow chat",
  created_by: { id: "admin" },
});

app.post("/dialogflow", async (req, res) => {
  const { text } = req.body;

  if (text === undefined || text.length == 0) {
    res.status(400).send({
      status: false,
      message: "Text is required",
    });
    return;
  }

  runSample(text)
    .then((text) => {
      channel.sendMessage({
        text: text,
        user: {
          id: "admin",
          image: "ignorethis",
          name: "Admin bot",
        },
      });
      res.json({
        status: true,
        text: text,
      });
    })
    .catch((err) => {
      console.log(err);
      res.json({
        status: false,
      });
    });
});

app.post("/auth", async (req, res) => {
  const username = req.body.username;
  
  const token = client.createToken(username);

  await client.updateUser({ id: username, name: username }, token);

  await channel.create();

  await channel.addMembers([username, "admin"]);

  await channel.sendMessage({
    text: "Welcome to this channel. Ask me few questions",
    user: { id: "admin" },
  });

  res.json({
    status: true,
    token,
    username,
  });
});

app.listen(port, () => console.log(`App listening on port ${port}!`));
package.json

{
 "name": "server",
 "version": "1.0.0",
 "main": "index.js",
 "license": "MIT",
 "dependencies": {
   "cors": "^2.8.5",
   "dialogflow": "^4.0.3",
   "dotenv": "^8.2.0",
   "express": "^4.17.1",
   "stream-chat": "^2.9.0",
   "uuid": "^8.3.2"
}
如果有人知道是什么导致了这个问题,我会非常感激,因为我迷路了

编辑:所以我按照建议修改了格式:

curl.exe-d“{'username':'username'}”-H“内容类型:application/json”-X POSThttp://localhost:5200/auth

现在,我在powershell的JSON.parse中的位置1处获得了JSON中的SyntaxError:Unexpected token',它转换为:

SyntaxError:服务器上位置1处JSON中的意外标记


再次感谢。

以下格式的curl命令在Linux平台上运行良好,但在Windows上失败

卷曲-d'{“用户名”:“用户名”}'

在Windows中,我们必须使用以下格式:

curl.exe-d“{'username':'username'}”-H”内容类型: application/json“-X POSThttp://localhost:5200/auth

基本上,shell将双引号视为语法的一部分,并将其剥离,因此需要对其进行转义

curl.exe-d“{\”用户名\“:\”用户名\“}”-H”内容类型: application/json“-X POSThttp://localhost:5200/auth


有关更多信息,请参阅。

非常感谢您的回复,我一定很喜欢windows lol。但现在我在位置1得到了JSON中的意外标记。是什么让o.o再次感谢你呢?PS,试试这个
curl.exe-d{“username\”:\“username\”}…
@boop它起作用了吗?嘿,谢谢你的跟进,我运行了命令:
curl.exe-d{“username\”:\“username\”}-H“内容类型:application/json”-X POSThttp://localhost:5200/auth
,输入“tFormat”的密码并获得错误:SyntaxError:意外标记-在JSON中的位置0处。最重要的是,我得到了:curl:(6)无法解析host:xml,而curl:(6)无法解析host:text。可能会放弃这个lol。@boop请尝试最后一个命令
curl.exe-d“{\”username\”:\“username\”}“-H”内容类型:application/json“-X POSThttp://localhost:5200/auth