如何将此POST重新请求正确地适配到plot.ly REST API



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

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

如何调整?

代码后:

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

添加内容类型标题:

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

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

您需要执行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,你应该在那里找到你的图表

来源:Intel Galileo的物联网,第3章,"绘制Galileo CPU温度"

相关内容

  • 没有找到相关文章

最新更新