Hello,
I was wondering what is the best librarie to use to do POST request.
I have tried using net.libraries but I have some issue with an “client got end of stream error”.
I have find that there is a http.requests.*; librarie.
Is there anyone who has already using this libraries to send a post request to a local server ?
Is there some way to troubeshoot a client ? I have try client.active and client.available but is seems that my client is never available. What does it mean ? Sorry if my question seems weird but i am new to processing and API.
Thanks in advance for your feedback.
here is my code :
Code
import http.requests.;
import processing.net.; //librarie for post request to the server
import processing.serial.*; //serial connexion to arduino
//import java.net.SocketException; //add for client end of stream (socket)
Client myClient; // Client object
final int SENSORCOUNT = 12; // This value must match SENSORCOUNT in your Arduino Code
String sensorValues[] = new String[SENSORCOUNT];
String junk;
String beginString = “begin”;
//String myServer = “localhost”;
String myServer = “127.0.0.1”;//server IP, warning don’t put port number here
String appKey = “140d461e-90cf-4c7c-9258-25da3838c36a”; //use the application key for admin that you have created on thingworx
String thingName = “myArduinoThing”;
String serviceName = “myArduinoService”;
String myURI = “http -v -j POST :8080/Thingworx/Things/” + thingName + “/Services/” + serviceName + " appKey:" + appKey;
//meme erreur
//String myURI = “POST/Thingworx/Things/” + thingName + “/Services/” + serviceName + " appKey:" + appKey;
String myHost = "Host: " + myServer;
//String myContent = “Content-type: text/html\n”; we need to send json type
String myContent = “Content-type: application/json\n”;
String sensorNames[] = {
“value1”, “value2”, “value3”, “value4”, “value5”, “value6”, “value7”, “value8”, “value9”, “value10”, “value11”, “value12”
}; //Enter your variable names (these must match the inputs in your Service)
int dataIn;
void setup() {
myClient = new Client(this, myServer, 8080);
print("my client: ",myClient);
print(“delay”);
}
int idx = SENSORCOUNT + 2;
void draw() {
/*start debug for client end of stream */
if (myClient.active() == true) {
println(“Client is active.”);
if (myClient.available() > 0) {
dataIn = myClient.read();
print(dataIn);
println(“Client is reading.”);
}
} else {
println(“Client is not active.”);
}
background(dataIn);
/*end debug for client end of stream */
for(int idx = 0; idx < SENSORCOUNT; idx++){
sensorValues[idx]="1024";
}
idx=SENSORCOUNT;
delay(100);
print("delay");
//When all sensor values have been read, send the info to ThingWorx
if (idx == SENSORCOUNT)
{
// myURI=trim(myURI);// ajout error end of stream happen below
print("my URI : ",myURI);
myClient.write(myURI);
for (int index = 0; index < SENSORCOUNT; index++)
{
myClient.write(sensorNames[index] + "=" + trim(sensorValues[index])+"");
}
myClient.write(" HTTP/1.1\n");
print("send http");
myClient.write(myHost + '\n');// erreur end of stream here ???
print("done host write");
delay(500);
myClient.write(myContent + '\n');// erreur end of stream here ???
delay(500);
println("Sending this REST call:");
print(myURI);
for (int index = 0; index < SENSORCOUNT; index++)
{
print(" " + sensorNames[index] + "=" + trim(sensorValues[index] + " "));
}
print(" HTTP/1.1\n");
print(myHost + '\n');
print(myContent + '\n');
print('\n');
delay(500);
idx = SENSORCOUNT + 2;
}
}