How to send HTTP POST request to a secure URL

Hello Smart People,

I am trying to send an HTTP POST to a secure server which uses OAuth token in the request header. However, it looks like my end server is not getting the request.

int BTN = D0;
int val = LOW;

void setup() {
   pinMode(BTN, INPUT);

   digitalWrite(BTN, LOW);
}

TCPClient client;
char server[] = "https://myserver.com/Spark";


void loop() {
  
  val = digitalRead(D0);
  
  if(val == HIGH) {
      
    Serial.begin(9600);
    delay(2000);
    Serial.println("connecting...");

    if (client.connect(server)) 
    {
      Serial.println("connected");
    
      client.println("Authorization: OAuth auth_token_here");
      client.println("POST /TEST4 HTTP/1.1");
      client.println("Host: myserver.com");
      client.println("Content-Type: text/json; charset=utf-8");
      client.println("Connection: Keep-Alive");
      client.println();
    } 
    else 
    {
      Serial.println("connection failed");
    }
  
  }
  
}

What am I missing here. TIA.

Hey @mswaleh
the TCPClient is a client to handle TCP connections. On top of this TCP connection there can be protocols like HTTP or HTTPS, but I think an HTTP(S)Client would be too big for the spark core. Especially HTTPS needs handling of certificates, which can get very complex.

1 Like

Hey @dominikkv, thanks for the reply. I am actually sending a request to an HTTPS server. Per your explaination, HTTPS is too heavy for the core but my project is based on an HTTPS server only (it is salesforce.com). I thought Spark Cloud can handle it as Core doesn’t directly send it to the third party server. I am quite new to Arduino and hence to Spark. Any other tips that I should follow. Thanks very much.

Hmm maybe you can use the following scenario:
You register an event with a callback to your server, your server does the HTTPS communication and sends the result back to your core. Note: Events are not ready yet. For more information see http://docs.spark.io/#/api/reading-data-from-a-core-events

You can also take a look at this topic: https://community.spark.io/t/sending-data-to-google-docs/48

1 Like

@mswaleh, I am working on something similar, but due to the Salesforce requirements, the logging in then retrieving the authorization token, etc., I am finding it easier to use an intermediate server running on Rackspace or Google. Expose the REST service there, then have it do the login to Salesforce and send the request to the REST service I exposed there.

In addition I don’t want the login information stored on the Spark or in the Spark cloud.

In the end I think it will be a lot more interesting once the Event’s are supported by Spark.

1 Like

@mtuckman , nice! This seems like a great approach. @mswaleh , I’d recommend going with that route if you can.

1 Like

thanks @mtuckman and @jgoggins for the help and input. will try and let you guys know. @mtuckman, pls let me know if you make progress on this route. Really appreciate it guys.

I should have it up and running this evening - got distracted with the Roomba to Robot conversion.

I am thinking of setting it up so when one of my salespeople complain in Chatter in our Salesforce org, the Roomba chases me down and tries to run me over to put me out of my misery! :wink:

1 Like

hehe…good one. My use case is kindda similar where when a certain object record goes under approval process, I wanna sound a small buzzer. It is a personal project as of now but I am planning to show it to my client. They’ll love it. I have done this using RPi but you know RPi is a big machine. Spark is little cute thing. :smile:

This subject is not completely clear for me. I thought the Spark core was able to make a secure TCP connection.
The connection with the Spark cloud is also realized with an https connection. But is it not possible to make a secure connection with https://api.cloudbase.io/?

HTTPS is not the only way to have a secure connection over TCP (as dominik​kv alluded to earlier). The core does encrypt communication between itself and the cloud, however it does not use the HTTPS protocol (details here: https://github.com/spark/core-communication-lib).

Unfortunately this means that you cannot speak to something over HTTPS directly from the core (unless someone writes an https library – unlikely due to the complexity). Thus, the way to do this is a. wait for event callbacks or b. use an intermediate server that handles HTTPS on behalf of your core.

1 Like

Could a cloud function enable https use from the core?

I know this would be less secure as the spark cloud would be able to see the data, but we already trust the cloud anyway?

This would enable easy integration into many third party services?

Hi @gorstj,

Good question! With the addition of Spark.publish, we will also soon be introducing webhooks - a listener you can set on the API to perform a http/https POST request to a url of your choosing. That functionality should be released in the next sprint or two. We’ve also been toying with something that would let you essentially make https requests through the cloud as a proxy and more, but that is a bit further down the road.

Thanks!
David

Hi @Dave,

What is the current status on these webhooks?

Hi @NickW,

I’m testing them in a small beta right now. They’re working, but I need some time to add some extra validation / protection so we don’t spam servers. :slight_smile: If you’d like to play around with them a bit, send me an email at david@spark.io :smile:

Thanks,
David

Where I can find the webhooks?

1 Like

Just catching up on mail now, and sending your beta invite soon :slight_smile:

Thanks,
David

@dave any chance I can get on that beta list too? :slight_smile:

@dave I’m doing a hardware hackathon at work this weekend and would like to show off the core.

If there is any chance you can get me on the beta list in the next day or two, I’d love to beta as well.

Now you can try to achieve https communication with this, albeit it is a work in progress:

@amanfredi @Dave @jgoggins

3 Likes