Hi Guys, I have a litttle experience with arduino and esp's, but now I cant figure this out.
Im trying to do a HTTP POST from a MKR WiFi 1010 to an API Endpoint. Right now I am testing with Reqbin ( https://reqbin.com/post-online ), where I get ["success":"true", 200 OK] with the same payload of my code, with their API POST Request online tool.
I am using the ArduinoHttpClient Simple Post Example, just using <WiFiNINA.h> instead of the original <WiFi101.h>
This is the code:
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
char serverAddress[] = "reqbin.com/echo/post/json"; // server address
int port = 8080;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(9600);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
Serial.print("Making POST request to ");
Serial.println(serverAddress);
String contentType = "application/x-www-form-urlencoded";
String postData = "name=Alice&age=12";
client.post("/", contentType, postData);
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
Serial.println("Wait five seconds");
delay(5000);
}
And Im getting this into Serial Monitor:
Attempting to connect to Network named: INFINITUM0739
SSID: INFINITUM0739
IP Address: 192.168.1.72
Making POST request to reqbin.com/echo/post/json
Status code: -2
Response:
Wait five seconds
Making POST request to reqbin.com/echo/post/json
Status code: -2
Response:
Wait five seconds
Making POST request to reqbin.com/echo/post/json
Could you help me to know, what I am missing?