如何正确地将此 POST 请求调整为 plot.ly REST API?

How to correctly adapt this POST resquest to plot.ly REST API?

我正在使用 Arduino Galileo 在 plot.ly 上使用 REST API 绘制图表。使用此代码

  client.println("POST /clientresp HTTP/1.1");
  //client.println("Host: 107.21.214.199");
  client.println("Host: plot.ly");
  client.println("User-Agent: Galileo/0.0.1");
  client.print("Content-Length: ");

  int content_length = 276 + username.length() + api_key.length() + temperaturesY.length() + timesX.length();
  client.println(content_length);
  client.println();
  client.print("version=2.3&origin=plot&platform=Galileo&un=");
  client.print(username);
  client.print("&key=");
  client.print(api_key);
  client.print("&args={\"x\":");
  client.print(timesX);
  client.print(",\"y\":");
  client.print(temperaturesY);
  client.print(",\"type\":\"scatter\",\"mode\":\"lines+markers\",\"visible\":true}&kwargs={\"filename\":\"galileo_temperature\",\"fileopt\":\"overwrite\",\"style\":{\"type\":\"line\"},\"layout\":{\"title\":\"Galileo CPU Temperature\"},\"world_readable\":true}");
  client.println();

我收到这个错误:

{"url": "", "message": "", "warning": "", "filename": "", "error": "Missing required POST parameters: platform un key origin args kwargs"}

如何适应这个?

您需要执行 POST 请求,例如:

POST /clientresp HTTP/1.1
Host: plotly.ly
User-Agent: Galileo/0.0.1
Content-Length: (needs to be calculated)

version=2.3&origin=plot&platform=Galileo&un=my_username&key=my_api_key&args={"x":[my_collected_x_values],"y":[my_collected_y_values],"type":"scatter","mode":"lines+markers","visible":true}&kwargs={"filename":"galileo_temperature","fileopt":"overwrite","style":{"type":"line"},"layout":{"title":"Galileo CPUTemperature"},"world_readable": true}

现在在你的草图中:

#include <Ethernet.h>

byte mac[] = { 0xDA, 0xAD, 0xBE, 0xEF, 0xFA, 0xEA };
EthernetClient client;

String username = "MY_USERNAME";
String api_key = "MY_API_KEY";

String temperaturesY = "[10,20,30,40,50]";
String timesX = "[0,1,2,3,4]";


void plotChart(){
  if (!client.connect("plot.ly", 80)) {
    Serial.println("... Couldn\'t connect to plotly's REST servers... ");
    return;
  }
  Serial.println("Connected to plotly's REST servers");
  Serial.println("Sending HTTP Post to plotly");

  client.println("POST /clientresp HTTP/1.1");
  client.println("Host: plot.ly");
  client.println("User-Agent: Galileo/0.0.1");
  client.print("Content-Length: ");

  int content_length = 276 + username.length() + api_key.length() + temperaturesY.length() + timesX.length();
  client.println(content_length);
  client.println();
  client.print("version=2.3&origin=plot&platform=Galileo&un=");
  client.print(username);
  client.print("&key=");
  client.print(api_key);
  client.print("&args={\"x\":");
  client.print(timesX);
  client.print(",\"y\":");
  client.print(temperaturesY);
  client.print(",\"type\":\"scatter\",\"mode\":\"lines+markers\",\"visible\":true}&kwargs={\"filename\":\"galileo_temperature\",\"fileopt\":\"overwrite\",\"style\":{\"type\":\"line\"},\"layout\":{\"title\":\"Galileo CPU Temperature\"},\"world_readable\":true}");
  client.println();
  Serial.println("Request sent. Waiting response..");
}

void setup() { 
  Serial.begin(9600);
  Ethernet.begin(mac);
  delay(5000);
  plotChart();
}

void loop() {
  //If Plotly response
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  delay(1000);
}

响应应该是这样的:

{"url": "http://plot.ly/~username/1", "message": "", "warning": "", "filename": ""}

如果您打开检索到的 url,您应该会在那里找到您的图表

来源:Internet of Things with Intel Galileo,第 3 章,"Plotting Galileo CPU temperature"

代码后:

client.println("POST /clientresp HTTP/1.1");
client.println("Host: plot.ly");
client.println("User-Agent: Galileo/0.0.1");

添加 content-type header:

client.println("Content-Type: application/x-www-form-urlencoded");

有了它,我能够成功绘制图表