HTTPS Request from Spark Core?

I see! Thanks for the help. I’m trying to use this example to get something off of another website now, and although it connects, it says “Bad Request.” What could that mean? Do you think there is something wrong with my code, or something with the internet connection?

my Code:

char server[] = “https://coinbase.com”;
TCPClient client;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

while(!Serial.available()) SPARK_WLAN_Loop();

if (client.connect(server, 80)) {
Serial.println(“connected”);
// Make a HTTP request:
client.println(“GET /api/v1/prices/spot_rate HTTP/1.0”);
client.println(“Host: https://coinbase.com/”);
client.println(“Content-Length: 0”);
client.println();
}

}

void loop()
{

if (client.available()) {
char c = client.read();
Serial.print©;
}

if (!client.connected())
{
Serial.println();
Serial.println(“disconnecting.”);
client.stop();
delay(5000);
}
}

client.println("Host: https://coinbase.com/");

The Spark Core cannot make a HTTPS connection. You’ll have to use a Proxy service for this, such as a PHP script running on a your webhost to forward the HTTP request for you to HTTPS, or wait for Spark Webhooks which will allow you to do the same thing :wink:

2 Likes

hmmm :frowning:okay. Do you think a beginner like me can do this, or it is really difficult? I have no clue where to even start! Thanks for all the help!

It’s not that difficult… you CAN learn :smile:

Basically you are trying to load this webpage to retrieve the Bit Coin spot price.

https://coinbase.com/api/v1/prices/spot_rate

While we could work through how to do that, I don’t think it’s the FIRST thing I would attempt to do. FIRST thing I would do is go look for an Bitcoin Price Index that’s not SSL encrypted… however, this is a bit risky if you are trying to buy and sell real-time with that data. If you are just making a simple [WOW and FLUTTER meter ¹][1] for BITCOIN prices… this will do just fine though… here’s one:

http://api.coindesk.com/v1/bpi/currentprice/USD.json


¹ not the best analogy but hey you probably learned something new…
[1]: http://en.wikipedia.org/wiki/Wow_and_flutter_measurement

:blush:I did learn something new with that analogy! hehe! And I am just doing it for the WOW and FLUTTER meter!

So instead of:

I can use char server = "http://api/coindesk.com";

and for

I can use client.println("GET /v1/bpi/currentprice/USD.json");

This should do it?

server, yes… path mostly correct… GET /v1/bpi/currentprice/USD.json HTTP/1.0 let me see if I can put together a full example for you… I’m trying out some new things I haven’t had a chance to play with yet, and it’s proving to be fun… yet challenging.

Hi @hassaan22

You don’t need the “http://” part, just the “api.coindesk.com” as the server name.

Then later on in the GET request line, you just need part after the server name as you have above.

1 Like

@hassaan22 here’s a good kick start for you :smile:

If you have any questions, please ask over there…

3 Likes

This is awesome! Thanks for the example @BDub

I was just working on JSON parsing with the with my Spark Core - glad to see a full working example to help me when I run into hiccups.

3 Likes