0

I have an Intel Edison running a Node.JS server that is printing everything I post to it into the console. I can successfully post to it using Postman and see the sent raw data in the console.

Now I'm using Processing to POST to it, which will fire off different events on the Node.JS server.

My problem is that I can't seem to successfully POST the raw body to the server, I've been trying to get this working for several hours already.

import processing.net.*; 

String url = "192.168.0.107:3000";
Client myClient;


void setup(){
    myClient = new Client(this, "192.168.0.107", 3000);
    myClient.write("POST / HTTP/1.1\n");
    myClient.write("Cache-Control: no-cache\n");
    myClient.write("Content-Type: text/plain\n");
    //Attempting to write the raw post body
    myClient.write("test");
    //2 newlines tells the server that we're done sending
    myClient.write("\n\n");
}

The console shows that the server received the POST, and the correct headers, but it doesn't show any data in it.

How do I specify the that "test" is the raw POST data?

The HTTP code from Postman:

POST  HTTP/1.1
Host: 192.168.0.107:3000
Content-Type: text/plain
Cache-Control: no-cache
Postman-Token: 6cab79ad-b43b-b4d3-963f-fad11523ec0b

test

The server output from a POST from Postman:

{ host: '192.168.0.107:3000',
  connection: 'keep-alive',
  'content-length': '4',
  'cache-control': 'no-cache',
  origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
  'content-type': 'text/plain',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
  'postman-token': 'd17676a6-98f4-917c-955c-7d8ef01bb024',
  accept: '*/*',
  'accept-encoding': 'gzip, deflate',
  'accept-language': 'en-US,en;q=0.8' }
test

The server output from my POST from Processing:

{ host: '192.168.0.107:3000',
  'cache-control': 'no-cache',
  'content-type': 'text/plain' }
{}
0andriy
  • 4,183
  • 1
  • 24
  • 37
Keith M
  • 1,199
  • 2
  • 18
  • 38

1 Answers1

0

I just figured out what was wrong, I needed to add the content-length header to tell the server how much data to listen for, and then a newline before the data.

Final code:

import processing.net.*; 

String url = "192.168.0.107:3000";
Client myClient;

void setup(){
    myClient = new Client(this, "192.168.0.107", 3000);
    myClient.write("POST / HTTP/1.1\n");
    myClient.write("Cache-Control: no-cache\n");
    myClient.write("Content-Type: text/plain\n");
    myClient.write("content-length: 4\n");     
    myClient.write("\n");
    myClient.write("test");
    myClient.write("\n\n");
}
Keith M
  • 1,199
  • 2
  • 18
  • 38