Faster HTTP request

Can I do that without making a local copy of the library? I’m using the Web IDE and it does not allow me to modify the library code. Maybe I"m doing something wrong.


ahh i see…

If your wanting to use the library version i updated to make it faster you will have to add the library manually anyway, i haven’t pushed it to the IDE. as its the normal one that i modified.

there are instructions on post #6 above that will get you started. Adding your own library is just about as easy as adding it from the IDE libraries anyway. Just look for the little + sign in the top right hand side of the IDE.

I’ve made it up to 10x faster. By parsing Http Content-Length header. You can find the code here:


Original Library : 10-12 sec
Modified Version: 600 ms

Using Photon.
Hitting a public API with average response time of 300ms on Desktop (Same WiFi Connection)

Feel free to test and let me know if there is any issues.

PS. Proof of concept. Code needs clean up :wink:


Maybe it’s me, but if I put the delay between the request to less than 2 seconds the core crash (red light) from times to times and at 0 second the stability is very low, the request are not regular.

@tchinou1 That should not happen. Try calling Spark.process() instead of delay.

I’ll apologize up front for the newbie question. I’m using the desktop version of Particle Dev so I can see the serial monitor. I’m trying to use the example and I’m getting back a status of 403. I’m able to curl and get back a valid response.

I’ve been taking shots on goal at changing the Content-Type and Accept values in the http_header. Can you give me a helpful nudge in the right direction to be look? Thanks.

@mojtabacazi - are you saying that we can get more reliable connections by using Spark.process() instead of delay() ?

How would I reproduce a delay(5) with Spark.process() ?

I have recently tried increasing my http request intervals and started having intermittent unreliability. I found an improvement in reliability with your code but it would good if there is a way to improve it further.

Unsigned long startTime = millis();
while ( millis() - startTime < myDelay)

Sort of thing…

1 Like

I am trying to do a POST request with this library with success. The issue is that it is taking very long. Some times 5445ms. it seems that the connection is waiting for more in the response body and hitting Error: Timeout while reading response. Can I do something to close the connection earlier?

Hi guys,

I’m having issues with the HTTP request on the Photon. If I use the library include in the Spark IDE the request is very slow, @Hootie81 If I use what you post a couple of month ago, I don’t compile and @mojtabacazi your version compile, but nothing happen (no serial monitor and I don’t think that the code run)
Before when I was on the Core I had a pretty good response time, but now it’s a couple a second…

I am running out of idea !

Thanks !

I am using HTTPClient extensively, and it is sloooooooow. A typical ‘GET’ from a random website is 5-7 seconds.
I tried Webhooks, and it is just too unreliable.
Maybe Particle management can raise the priority for a more sophisticated HTTPClient library, something that is fundamental for what is primarily an IOT device. Lots of fancy features… but fundamentals?

Sounds like you’re running into a TCPClient timeout? I’m using the develop branch and seeing responses of 500ms. Working on webhooks at the moment and also finding they’re too unreliable which is a real shame.


What I don’t understand is that before, the response time that I had was something like 500ms for me too. But, now for an unclear reason it’s a couple of second…

Can you point me to the dev branch that you are using? What I’m doing is fairly simple, it is worth a try.

I meant the firmware develop branch for compiling offline. Actually once the system1 and system2 is updated tcpclient works as expected and you can compile code in the cloud…

@JumpMaster I am not sure to understand, can I use the develop branch ? If yes, how ?

Take a look at this topic…

Interesting, but for I want to share my project with a couple of friends and I don’t want them to have to compile locally. I don’t get it, according to the web IDE HttpClient is the most used library and can’t find any topic about the slow response time… Have I miss something ?

The problem is in firmware 0.4.4 and earlier on the Photon. The TCPClient used in HttpClient is not detecting the server disconnecting so instead the connection is closed after a 5 second timeout. This is fixed in the next firmware release. To work around the issue for now copy HttpClient.cpp and HttpClient.h adding them to your program. Change line 5 as below and reduce the timeout. It need to be long enough that your request completes, try 2000 and then 1000.

static const uint16_t TIMEOUT = 5000; // Allow maximum 5s between data packets.

I fixed this in HttpClient by parsing the content length of the response into an integer and exited my loop after the response body buffer was equal to the content length. I can post the code if anyone is interested.