Hey y’all, I’m making a processing sketch to interact with an API that I wrote in node.js using express.js as my web service. When sending GET requests, all is splendid, due to the information being contained in the URL, but when attempting to send a POST request, the data within the request seems to only be in a plain text format, which is unable to be parsed properly by my API.
Here’s a simplified version of the server in node (you must install node (obviously) , express, and body-parser):
const express = require('express');
const app = express();
const parser = require('body-parser');
const port = 8080;
//app.use(parser.json('text/plain')); //set the json content type option for body-parser
app.use(parser.text());
app.post('/', function(request, response){ //this method responds to requests made to the server URL with no additional paths
console.log(JSON.stringify(request.body, null, 2)); //log the request body (parsed with body-parser) (stringified with indentation format to make reading it easier)
response.send(JSON.stringify(request.body)); //send the received body (casted to string) pack to client
response.end(); //terminate response
});
app.listen(port, function(){
console.log('server is listening on ' + port);
}); //make the server listen on specified port
And here is the cut-down sketch (must install http.requests library by @shiffman and @runemadsen ):
import http.requests.*;
public void setup(){
PostRequest post = new PostRequest("http://localhost:8080/"); //make new POST request object directed to local server on port 8080
post.addHeader("Content-Type", "text/plain");
post.addData("firstValue", "someValue"); //add data to request
post.addData("secondValue", "anotherValue");
post.send(); //send request
println(post.getContent()); //print response
noLoop();
}
public void draw(){
}
this sketch receives the response:
“firstValue=someValue&secondValue=anotherValue”
This is unable to be parsed by body-parser and returns “undefined” for “request.body.firstValue”
when the Content-Type in the POST header is changed to
post.addHeader(“Content-Type”, “application/json”);
There is no content in the body of the received request whatsoever:
{}
and, when in the server the JSON body-parser is used with:
app.use(parser.json('application/json'));
I can tell that it receives the data, but it is unable to be parsed. I get the following error:
SyntaxError: Unexpected token f in JSON at position 0
at JSON.parse ()
at createStrictSyntaxError (C:\Users\nosson\nodeServer\node_modules\body-parser\lib\types\json.js:158:10)
at parse (C:\Users\nosson\nodeServer\node_modules\body-parser\lib\types\json.js:83:15)
at C:\Users\nosson\nodeServer\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\Users\nosson\nodeServer\node_modules\raw-body\index.js:224:16)
at done (C:\Users\nosson\nodeServer\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\nosson\nodeServer\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
The unexpected token f is certainly referring to “firstValue”, but it is not recognized as being part of a JSON object and therefore cannot be properly parsed.
So, if you know anything about how to manipulate http.requests to send proper JSON, or how to have body-parser properly parse the text/plain content, please lend your help, I’d greatly appreciate it