How to properly use geocod.io api

I’m trying to access the geocod.io api for geocoding conversion. I’ve received an api key and used the following http:// web browser request and seen that it works great:
http://api.geocod.io/v1/geocode?q=my_zip&fields=timezone&api_key=my_api_key

where my_zip and my_api_key are replaced with the appropriate values.

I’m having trouble getting the appropriate Arduino sequence of client prints.

The geocod.io documentation says:

The base API url is https://api.geocod.io/v1/.

You can also use Geocodio over plain HTTP, but it’s not recommended.

All HTTP responses (including errors) are returned with JSON-formatted output.

I’ve tried the following:
if ( WiFi.status() == WL_CONNECTED) {

if (client.connect({46,4,34,210}, 80)) {
Serial.println(“connected to geocod.io server”);
// Make a HTTP request:
client.println(“GET /v1/geocode?q=my_zip&fields=timezone&api_key=my_api_key HTTP/1.1”);
client.println(“Host: api.geocod.io/”);
client.println(“User-Agent: me@my_address”);
client.println(“Accept: application/json”);
client.println(“Connection: close”);
client.println();
Serial.println(“Done with client.println routine calls for geocod.io.”);
latlon_requested = true;
latlon_attempted_reads = 0;
}
else {
Serial.println(“Failed to connect to geocod.io server!”);
latlon_timeout = true;
}
}

I’ve got the ip address (retrieved from nslookup), because when I put api.geocod.io there, it wouldn’t connect. When I do client.read() calls, I end up seeing text like this:

HTTP/1.1 400 Bad Request
Server: nginx/1.6.2
Date: Tue, 07 Jun 2016 01:36:05 GMT
Content-Type: text/html
Content-Length: 172
Connection: close

400 Bad Request

400 Bad Request


nginx/1.6.2 ---------------------------------------------

I get the feeling I’m not actually talking to the api engine. Is it clear from the web browser line (that does work) how to put together the Arduino sequence?

Thanks.

co_buckfast

Hi there,

Mathias from geocod.io here. Exciting to see that you're using geocod.io with an Arduino!

It looks like you are hitting the server, but that an unexpected HTTP header was set.

It looks like this could be the culprit:
client.println("Host: api.geocod.io/");

should be changed to (removing the forward slash):
client.println("Host: api.geocod.io");

I noticed that you are using the IP address directly, which is fine for now. But please note that it could potentially change in the future.

Currently we have two load balancers, located at 46.4.34.210 and 176.9.155.170.

Another thing I wanted to mention is to make sure to url encode the "q" query parameter if you are going to pass anything else than zip codes.

Feel free to reach out in our online chat on geocod.io as well, and I'll be happy to help out.

Mathias

Feel free to reach out in our online chat on geocod.io as well, and I'll be happy to help out.

It's good to see someone actually support a product that they want people to use.