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

**URL:** <https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759>\
**Category:** Libraries\
**Created:** [July 17, 2019, 9:30am UTC](https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759 "2019-07-17T09:30:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)\
**Post date:** [July 17, 2019, 9:30am UTC](https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759/1 "2019-07-17T09:30:03Z")

</div>

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](http://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;
> }
> 
> ```
> 
> }

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [July 17, 2019, 4:15pm UTC](https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759/2 "2019-07-17T16:15:44Z")

</div>

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](https://www.getpostman.com/)

---

<div class="post-metadata">

**Author:** ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)\
**Post date:** [July 18, 2019, 7:06am UTC](https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759/3 "2019-07-18T07:06:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [July 18, 2019, 6:18pm UTC](https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759/4 "2019-07-18T18:18:13Z")

</div>

> [@Dupond](#):
>
> I am able to send the POST request with HTTPIE

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

What kind of errors do you get back?

---

<div class="post-metadata">

**Author:** ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)\
**Post date:** [July 18, 2019, 7:48pm UTC](https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759/5 "2019-07-18T19:48:37Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5f5d6c994a77c1c7e9e03ffa357fa4fabe5204cb.jpeg)

---

<div class="post-metadata">

**Author:** ![phillipsj](https://avatars.discourse-cdn.com/v4/letter/p/a87d85/32.png) [@phillipsj](https://discourse.processing.org/u/phillipsj)\
**Post date:** [July 29, 2019, 2:35pm UTC](https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759/6 "2019-07-29T14:35:42Z")

</div>

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](http://import.processing.net) statement at the top?

```auto
import.processing.net.*;

```

---

<div class="post-metadata">

**Author:** ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)\
**Post date:** [July 30, 2019, 8:00am UTC](https://discourse.processing.org/t/rest-api-client-and-post-request-with-processing-net-libraries/12759/7 "2019-07-30T08:00:34Z")

</div>

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](https://www.processing.org/reference/libraries/net/Client_write_.html)
