3

I want to make an Http Post request with Bearer Authentication to my api using my Arduino Mkr Wifi 1010, but I couldn't find the appropriate library to do something like this (code generated with one made with Postman):

 POST /api/v1/rilevamento/transaction/commit HTTP/1.1
Host: www.....:8082
Authorization: Bearer eyJhb.......
Content-Type: application/json
Content-Length: 233
{
 "id": 0,
 "dispositivo": "sesto",
 "nomeutente": "MyName",
 "cognomeutente": "MySurname",
 "mailutente": "[email protected]",
 "data": "2020年11月23日T09:23:03.142+00:00",
 "temperatura": 37.5
}

Can you tell me where to find this library? I tried WifiNiNa httpClient, ArduinoHttpClient, HTTPClient and other but I couldn't get any response or even enter in the method of my api on debug.

asked Nov 24, 2020 at 15:15

1 Answer 1

6

The "Authorization" is simply an HTTP header. So add it in your request like:

http.addHeader("Authorization", token);

The value of "token" is just the string "Bearer " followed by your authorization string. Don't neglect the SPACE after the word "Bearer". It will look like:

"Bearer eyJhb......"

On the Arduino MKR WiFi 1010 you use the WiFiNINA library which is slightly different. Here is some example code:

 if (client.connect(server, 80)) {
 client.println("POST /test/post.php HTTP/1.1");
 client.println("Host: www.targetserver.com");
 client.println(authorizationHeader);
 client.println("Content-Type: application/x-www-form-urlencoded");
 client.print("Content-Length: ");
 client.println(postData.length());
 client.println();
 client.print(postData);
 }

The variable authorizationHeader will have the "Authorization: Bearer xxxxx...." authorization string.

Or you can replace the client.println(authorizationHeader); by:

client.print("Authorization: Bearer ");
client.print(token);

to avoid dealing with concatenations, etc.

client can be an instance of WiFiSSLClient which works with both WiFi101 and WiFiNINA.

answered Nov 24, 2020 at 15:43
2
  • I believe this is made with HTTPClient which is for ESP modules, can I use it with my MKR Wifi? Commented Nov 24, 2020 at 15:51
  • With the MKR WiFi 1010 you use the WiFiNINA library which is slightly different. I've updated my answer with the relevant details. Commented Nov 24, 2020 at 17:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.