【问题标题】:POST raw to server ProcessingPOST 原始到服务器处理
【发布时间】:2018-01-20 03:55:11
【问题描述】:

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

现在我正在使用处理向它发布,这将在 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 和正确的标头,但其中没有显示任何数据。

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

来自 Postman 的 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

来自 Postman 的 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 来自 Processing 的服务器输出:

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

【问题讨论】:

    标签: http http-headers processing postman intel-edison


    【解决方案1】:

    我刚刚发现出了什么问题,我需要添加 content-length 标头来告诉服务器要侦听多少数据,然后在数据之前添加一个换行符。

    最终代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-07
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      相关资源
      最近更新 更多