Ich habe eine Express-Anwendung, die als eine einfache erholsame API konzipiert ist. das ist mein app.js Datei, um den Körper Parser MiddlewareExpress Unerwartete Token k
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
mit bodyparser 1.15.2
das ist mein Grund Routing-Datei
...
r.post('/search/:keyword', function(req, res){
var __keyword = req.params.keyword,
console.log(req.body);
Ich verwende Postbote Zugabe für Senden von Anfragen, wenn ich die rohe Registerkarte für das Senden der Anfrage mit der Anwendung/JSON Header das Ergebnis verwenden, ist in Ordnung. Der Code auf diese Weise von dem Senden von Anforderungen erzeugt wie folgt aussieht
POST /api/search/mykeyword HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
{
"key": "value"
}
wenn in Postbote die x-form-urlencoded Option Ich erhalte diesen Fehler
SyntaxError: Unexpected token k
at parse (D:<projectpath>\node_modules\body-parser\lib\types\json.js:83:15)
at D:<projectpath>\node_modules\body-parser\lib\read.js:116:18
at invokeCallback (D:<projectpath>\node_modules\body-parser\node_modules\raw-body\index.js:262:16)
at done (D:<projectpath>\node_modules\body-parser\node_modules\raw-body\index.js:251:7)
at IncomingMessage.onEnd (D:<projectpath>\node_modules\body-parser\node_modules\raw-body\index.js:307:7)
at emitNone (events.js:67:13)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:913:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
die Anfrage dann wie dieses
siehtPOST /api/search/mykeyword HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
{
"key": "value"
}
Für mich sieht das genau gleich aus, aber es produziert diesen Fehler? Ist das ein Bug in bodyparser, Express oder Postman? Oder ist das mein Fehler? Wenn nur
app.use(bodyParser.urlencoded({extended: true}));
das req.body Objekt ist immer leer, unabhängig davon, welche der beschriebenen Art und Weise ruft ich
Ich glaube, Sie den Körper-Typ setzen als 'x-www-form-urlencoded' und Content-type als' application/json'. Kannst du den Inhaltstyp in Kopfzeilen auf "application/x-www-form-urlencoded" ändern und überprüfen? –