Reputation: 21
Anyone have a clue why I'm getting this error when running this request on powershell?
curl.exe -d '{"username" : "username"}' -H "Content-Type: application/json" -X POST http://localhost:5200/auth
index.js file:
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"
}
I'd really appreciate if anyone has a clue on what could be causing the issue, cause I'm lost
edit: So I fixed the format as suggested to:
curl.exe -d "{'username' : 'username'}" -H "Content-Type: application/json" -X POST http://localhost:5200/auth
and now I get SyntaxError: Unexpected token ' in JSON at position 1
at JSON.parse in powershell which translates to:
SyntaxError: Unexpected token ' in JSON at position 1 on the server
Thanks again.
Upvotes: 2
Views: 1035
Reputation: 2232
The curl command with the following format works well on Linux platform but it fails on Windows
curl... -d '{"username" : "username"}' ...
From the Windows we have to use the following format:
curl.exe -d "{'username' : 'username'}" -H "Content-Type: application/json" -X POST http://localhost:5200/auth
Basically shell treats the double quotes as a part of the syntax and strips them, so you need to escape them
curl.exe -d "{\"username\":\ \"username\"}" -H "Content-Type: application/json" -X POST http://localhost:5200/auth
See this answer for more information.
Upvotes: 0