HTTP Authorization

Hello guys,

I have a Arduino Mega, and I am trying to make a HTTP GET to do an Authorization in my server.

I am trying in this way:

// Make a HTTP request:
client.println("GET /api/fp/ HTTP/1.0\n");
client.println("Authorization: ");
client.println("Basic YmRjYzE0MDE2ZDkxNjNmNjg5MWIxYzc1NWM3YjRhNzY6RDJNQVBsTmpBRGtMWkFCaw==");
client.println();

But I am still receiving an "Not authorized" error.

How I can do this?

Thank you very much

Insure your username and password is correct and Base 64 encoded.

It looks ok to me except for that.

edit: My bad. Here is the problem. The first client.println should be client.print

client.print("Authorization: ");
client.println("Basic YmRjYzE0MDE2ZDkxNjNmNjg5MWIxYzc1NWM3YjRhNzY6RDJNQVBsTmpBRGtMWkFCaw==");
client.println();

Guys,

I tried using POST e now It's ok.

Strange because in my browser I use GET.

Anyway, thank you everybody

If you want to use GET, remove the "\n" from the end of that client.println.

        // Make a HTTP request:
         client.println("GET /api/fp/ HTTP/1.0");

Otherwise, it interprets everything after that line as the body (POST);

Hello,

Yes, all of you was correct.

Now I am using in this way:

client.println("GET /api/fp/to/calendar/23a31003d6d904c81a3c77f62185db9c/eventos HTTP/1.0");
client.println("Authorization: Basic YmRjYzE0MDE2ZDkxNjNmNjg5MWIxYzc1NWM3YjRhNzY6RDJNQVBsTmpBRGtMWkFCaw==");

And It is working perfect :smiley:

edit: I just have one more question. Why when I use HTTP/1.1 I receive a Bad Request?
And when I send a HTTP/1.0 the answer comes in HTTP/1.1

Some servers require a "Connection: close" in the header for HTTP/1.1. Replace the final client.println() with this:

client.println("Connection: close\r\n");

Hello,

For tell the truth the Connection: close, didn't solve my problem.

So I searched in the google and I figured out, that I have to put the Host, like this:

client.println("GET /api/fp/ HTTP/1.1");
client.println("Host: 10.9.9.11");
client.println("Authorization: Basic YmRjYzE0MDE2ZDkxNjNmNjg5MWIxYzc1NWM3YjRhNzY6RDJNQVBsTmpBRGtMWkFCaw==");
client.println();

And now the HTTP/1.1 It's working perfectly, thank you all guys for the answers.

So, I am an absolute beginner in C++ and Arduino.

Now I have a another question, when I send the request above I receive this answer from my server:

HTTP/1.1 200 OK
Date: Sat, 10 Nov 2012 18:16:48 GMT
Server: Apache
X-Powered-By: PHP/5.3.3-7+squeeze14
Set-Cookie: PHPSESSID=v7kqmfqtd7tlpe8nk8h6kc2jc5; path=/
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Content-Length: 140
Content-Type: application/json

{"request":{"type":"GET","elapsed_time":"0.0093"},"data":{"id":"v7kqmfqtd7tlpe8nk8h6kc2jc5","name":"PHPSESSID","msg":"Auth Successfull."}}

So, now I need to use this "PHPSESSID=v7kqmfqtd7tlpe8nk8h6kc2jc5" to stay sending request to my server. How I can get this PHPSESSID from the answer and put again in my Arduino code?

Thank you very much!

Just what I was looking for! Tinkering around a couple of evenings now, finding out what the correct syntax for the Authorization: Basic header should be.
Success finally, putting the Authorization: Basic after the Host.