2016-01-17 22 views
0

我有一个运行Node.JS服务器的英特尔Edison,它正在打印我发布到控制台中的所有内容。我可以使用Postman成功发布,并在控制台中查看发送的原始数据。POST原始到服务器处理

现在我正在使用Processing来POST,它会触发Node.JS服务器上的不同事件。

我的问题是,我似乎无法成功发布到服务器的原始机构,我一直试图让这工作已经几个小时了。

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"); 
} 

控制台显示服务器收到POST和正确的标题,但它没有显示任何数据。

如何指定“test”是原始POST数据?

从邮差的HTTP代码:

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 

从POST的输出从邮差服务器:

{ 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 

从我的POST输出服务器从处理:

{ host: '192.168.0.107:3000', 
    'cache-control': 'no-cache', 
    'content-type': 'text/plain' } 
{} 

回答

0

我只是找出了什么是错误的,我需要添加内容长度头来告诉服务器有多少数据要听f或者,然后在数据之前换行。

最终代码:

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"); 
}