Webhook sent directly from photon

Just having some trouble getting a webhook to work from my photon. I realize that I can use the integrated webhooks sent from the particle cloud via a particle.publish but I would like to send the webhook directly from my photon because the server that I am sending it to is locally hosted and not exposed to the world wide web… I have been trying to use the http client library with no success. I wanted to turn to the experts since I clearly am not getting this to send through… GO easy on me…

if i paste this URL in my browser and hit go it successfully fires

http://ipaddress/folder/version2/madeupfolder/:randomlettersandnumbers123456:?access_token=more-random-letters-and-numbers-hyphens

This also works as a curl request…

This is my photon code I was trying to trigger it to no avail… help

// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>


HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

void setup() {
    
}

void loop() {

    request.hostname = "http://ipaddress";
    request.port = 80;
    request.path = "/folder/version2/madeupfolder/";
    request.body = ":randomlettersandnumbers123456:?access_token=more-random-letters-and-numbers-hyphens";
    http.post(request, response, headers);
    delay(20000);
}

First, if you want to talk to the servier directly it’s not really called a webhook.

But your actual problem is that your request.hostname is wrong for two reasons

  • the scheme/protcol denominator (http://) is not part of the host name
  • an IP address is no name but an address hence it should be supplied via the request.ip field (the HTTPClient library has been improved to deal with that common misconception, but doing it correct in the first place is the better practice)
3 Likes

In addition to what @ScruffR said, your curl example has the data as part of the path but you are putting your data into the body in the httpclient code. The curl example seems to use the GET method but the httpclient code uses a POST method. After you correct the issues @ScruffR pointed out, you may want to look at how you are adding in the data. Do you truly want GET or POST?

2 Likes

I have the same issue. I have an example which will http.get a public url ok. But if I switch to my local network, it fails. I am starting to think there is a bug in httpclient. I’m kind of surprised we’re the first to run into such a simple thing.

If you show what you are doing?
I’ve successfully used HTTPClient in a local setting too, so it’s more likely something specific to your setup.

That would be fantastic. I’ll post the code I have tonight.

Thanks

Thanks everyone for the help I knew it would just be a silly way I formatted it. I tried moving the path and the body and different combinations last night before I asked but it was the http:// that threw it off… also putting everything in the path with no body was the right way. I tried the request.ip instead but it spit data type errors so I’m not sure how I should have inputted that differently. I also sent it as a get request no matter how much I read on that stuff I still get the post and get functions confused haha.

1 Like

The IP address should be provided as such and not as string :wink:

e.g like this

  IPAddress ip(192, 168, 0, 10);
  request.ip = ip;

I also use httpclient to communicate with a local web server. I wrote the server API and I have no problems receiving the data. Post the code and I’m sure we can help.

1 Like

Are there any examples that you guys are aware of that do the opposite… For example post a web request to the photon and call a function. Kind of like particle.function but all locally ran? I was able to get this working via MQTT fine but I feel like a web request would work better since the device I will be calling it from doesnt directly support mqtt.

You could give this a try
https://build.particle.io/libs/WebServer/0.0.2

oh man that definitely looks like greek to me haha especially with no sample code in the library… I’m too much of a newby to interpret all that… It looks like you could run the particle cloud locally too but it appears what I found was abandoned and depreciated and I couldn’t get it to install with the newer versions of node and npm. Is there anywhere that this concept has been kept up and developed?

It would be possible to trigger the photon to do something using a local network using TCPclient. It wouldn’t be using the http protocol. I do this by sending JSON messages between photons on a local network. Seems simpler than using a webserver.

1 Like

I have created a new thread, with source code posted. Please see WebClient.get does not work (for me) over local 192.168.x.x network

Thanks for any assistance.