Question regarding the WiFiClient.println()

Hi all,

I just have one question regarding the WiFiClient.peintln().

The information I sent to the server is as following:

client.println("ThisIsEncodedBase64");

And whenever I print out the number of bytes I sent, it always contains 3 more bytes than it should be.

The reason I wanna ask is the following code:

    _networkClient->println("POST /notification2/subscriptions HTTP/1.1");  
    _networkClient->print("Host: ");
    _networkClient->println(URL);
    _networkClient->print("Authorization: Basic "); 
    _networkClient->println(_base64);
    _networkClient->println("Content-Type: application/json");
    _networkClient->print("Content-Length: ");
    _networkClient->println(body2send.length());
    _networkClient->println("Accept: application/json");
    _networkClient->println();
    _networkClient->println(body2send);

The _base64 is a variable and the code above doesn't work because:

{"error":"security/Unauthorized","message":"Failed to decode basic authentication token","info":"https://www.cumulocity.com/guides/reference/rest-implementation//#a-name-error-reporting-a-error-reporting"}

However, with the following code everything is fine...

    _networkClient->println("POST /notification2/subscriptions HTTP/1.1");  
    _networkClient->print("Host: ");
    _networkClient->println(URL);
    _networkClient->println("Authorization: Basic dGhpc2lzdGVzdDpoZWxsb3dvcmxk");
    //_networkClient->println(_base64);
    _networkClient->println("Content-Type: application/json");
    _networkClient->print("Content-Length: ");
    _networkClient->println(body2send.length());
    _networkClient->println("Accept: application/json");
    _networkClient->println();
    _networkClient->println(body2send);

It seems like that _networkClient->println(_base64); doesn't work properly...

Anyone know how to fix this?

Thanks!

display your data in HEX.
I assume you will see a 0x10 and 0x13 at the end due to println.

use print instead.

Try

client.print("ThisIsEncodedBase64");

Sure? println() just adds 2 more (Carriage Return and Line feed, or CR & LF, or 0x0D & 0x0A), not 3...

But it is part of the http header.

    _networkClient.println("POST /notification2/subscriptions HTTP/1.1");  
    _networkClient.print("Host: ");
    _networkClient.println(URL);
    _networkClient.print("Authorization: Basic ");
    _networkClient.println(_base64);
    _networkClient.println("Content-Type: application/json");
    _networkClient.print("Content-Length: ");
    _networkClient.println(body2send.length());
    _networkClient.println("Accept: application/json");

As you can see from above that URL and _base64 are parameters. I really don't know how to control client.print(_base64) such that the Carriage Return would be located at the new line...

But it is part of the http header.

    _networkClient.println("POST /notification2/subscriptions HTTP/1.1");  
    _networkClient.print("Host: ");
    _networkClient.println(URL);
    _networkClient.print("Authorization: Basic ");
    _networkClient.println(_base64);
    _networkClient.println("Content-Type: application/json");
    _networkClient.print("Content-Length: ");
    _networkClient.println(body2send.length());
    _networkClient.println("Accept: application/json");

As you can see from above that URL and _base64 are parameters. I really don't know how to control client.print(_base64) such that the Carriage Return would be located at the new line...

you should have tell us that this data is part of the http header fields.

based on these line I suspect that the content of _base64 is not correct.
As mentioned already, print it in HEX to serial and compare bytewise what is happening. A full compileable example can lead you to the root cause.

I have ready printed the data using Serial.print(_base64); and it displayed the correct data. Why should I print it in HEX byte by byte? And how should I achieve it?

Sorry I am not very familiar with the C++. May I ask what is the relation between the printable characters and these characters in HEX form?

    _networkClient.print("Authorization: Basic ");
    _networkClient.println(_base64);

So now you finally posted your code! Ok, it comes out you're trying to send a base64-encoded auth token over an html request: if the reply is an "unauthorized" error, it means you had something bad with the token you're sending to OpenAPI, so double check the _base64 variable content.

And about Basic Authentication with OpenAPI, carefully read HERE.

PS: for the 3 "extra" characters, I still don't know what are you talking about and where/how you saw that.

Hi all,

I finally know where the issue is! It is the version of the platform that leads the "unauthorized".

Thank you all!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.