HTTP POST Request sending incorrect Content-Type

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

1 Like

“SyntaxError: Unexpected token f in JSON at position 0”

That is the “f” in “firstValue”. A guess: it looks like you need to actually send valid JSON. You are just sending two values, which is invalid:

The data needs to be serialized in https://www.json.org/ syntax.

So if instead of addData(“firstValue”) you sent something like addData("[“firstValue’”]") that is valid JSON. Depending on your data, you may need [] or {}, with values or key-values pairs.

There was a past discussion of using Requests more easily using Processing’s built-in JSON tools:

1 Like

@jeremydouglass thanks for your reply.

I am aware of the necessary format of JSON, and it would seem that most servers utilizing post requests only use JSON in their backend, not plain text. Curiously, I HAVE had POST requests work in other coding projects of mine using just post.addData(“name”, “value”);
see: Plaque/Zmanim.pde at master · nossonCotlar/Plaque · GitHub (line 44)

Additionally, my method of adding data to a post request (presumably automatically serialized into JSON) is supported in the http.requests documentation: GitHub - runemadsen/HTTP-Requests-for-Processing

I appreciate your suggested fixes, but none of them put my data in the desired JSON format which is:

{
“firstValue”:“someValue”,
“secondValue”:“anotherValue”
}

There is a method of doing this without adding any extra shenanigans, as proven with my github code that I have linked, and ideally I’d like to find a way to continue using post.addData() for this.
If that’s impossible for some reason, I’ll look into more long-winded methods.
Thanks again

Solved:
in the node server code, instead of using

app.use(parser.text(‘text/plain’));

I use

app.use(parser.urlencoded({ extended: false }));

and in the Processing sketch, the header is set as follows:

post.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);

this type seems to jive with the request that Processing sends :slight_smile:
And, I can continue using the dead-easy way to add data:

PostRequest post = new PostRequest("http://localhost:8080/"); //make new POST request object directed to local server on port 8080
  
  post.addHeader("Content-Type", "application/x-www-form-urlencoded");
  post.addData("firstValue", "someValue"); //add data to request
  post.addData("secondValue", "anotherValue");
  post.send(); //send request

and I get the returned data:

{“firstValue”:“someValue”,“secondValue”:“anotherValue”}

yay <3

3 Likes