Go Down

Topic: API returns an odd 301 error? (Read 637 times) previous topic - next topic


Jun 20, 2019, 08:15 pm Last Edit: Jun 21, 2019, 05:45 pm by ZainS
Having had good luck with the usual example code and openweathermap.com, I turned my MKR 1010 Wifi onto magicseaweed.com. And am getting an error back from the server that I cannot figure out. It seems to be telling me that the API URL has moved, but the link it provides back is the same one I have been using. Relevant code snips and API response follow below. Any advice appreciated. Thanks

Code snips:

In init()
WiFiClient client;
const char server[] = "api.magicseaweed.com";
String apiKey = SECRET_APIKEY;

In loop()
... after checking the wifi is till up and all that ...
Serial.println("Attempting to connect to server...");
  // if there's a successful connection:
  if (client.connect(server, 80)) {                                         
    client.println("GET /api/"  + apiKey + "/forecast/?spot_id=402 HTTP/1.1");
    client.println("Host: api.magicseaweed.com");
    client.println("User-Agent: ArduinoWiFi/1.2.1");
    client.println("Connection: close");

    Serial.println("Request Sent...");

... etc etc... and a check for a timeout then the usual example code for reading from the client...

This is what comes back:

HTTP/1.1 301 Moved Permanently
Date: Thu, 20 Jun 2019 18:14:06 GMT
Server: Apache/2.2.15 (CentOS)
Location: http://magicseaweed.com/api/*************************/forecast/?spot_id=402
Content-Length: 376
Connection: close
Content-Type: text/html; charset=iso-8859-1

<title>301 Moved Permanently</title>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://magicseaweed.com/api/***************/forecast/?spot_id=402">here</a>.</p>
<address>Apache/2.2.15 (CentOS) Server at api.magicseaweed.com Port 80</address>


is http supported on the server? doesn't t require https?


It doesn't seem to? (Please pardon the following noob-ness):

- Their API URL examples don't use it. https://magicseaweed.com/developer/forecast-api
- I can test the API URL in browswer without https and it responds fine
- at quick email to tech support at magic seaweed asking if I needed SSL or the like did not garner an "oh yea!"

However, since I'm a wandering mechanical engineer who wouldn't know how to change my code over to try HTTPS, how would I do it? I presume that "HTTP/1.1" there at the end needs altering, but to what?

Thanks again.



There is a slight discrepancy in the host name. See if changing "api.magicseaweed.com" to "magicseaweed.com". There are two lines of concern.

Code: [Select]
const char server[] = "api.magicseaweed.com";
client.println("Host: api.magicseaweed.com");


I was just about to type "tried it already before posting original question," but obviously I failed to try it properly, as when I just did it again it seems to work. Sigh...

Thanks for the help!


Go Up