[Rest API] Client and POST request with Processing .net libraries

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

}

1 Like

Are you talking with an API you made, or an external, Third-Party API?

Networking can be difficult if you’re new to it, for sure. Maybe start with making sure you can connect to the API with something like Postman

1 Like

I want to send a POST request to ThingWorx REST API to access my thingworx server.
I am able to send the POST request with HTTPIE to my thingworx server but with processing I am not able to do this and I don’t understand understand where is the issue.
I thinks it is when i am using write fonctions with my client but I have no clue to solve this.

I will look for POSTman it is not the first time I heard this name. I found it many time during my research.

Oh, interesting. So it sounds like it isn’t a problem with the API.

What kind of errors do you get back?

I get a first error which is Client got end of stream error follow by many null pointer exception.
I try to debug and I think it is due to the fonction write that i use with my client.connect_processing

Not sure if it has any bearing on your code, but do you need to include the asterisk at the end of your import.processing.net statement at the top?

import.processing.net.*;

Hi @phillipsj
This is the syntaxe that you need to use when you import a librarie.
You can see it on all the example. For instance here :

https://www.processing.org/reference/libraries/net/Client_write_.html