0

I would like to ask about sending data from arduino/esp to .net App service running on Azure. I am sending JSON to API with "list" of measurements (simple JSON, Id and Value).

Over PostMan I don't have any issue, but over Arduino I am facing issue that I receive result code 415. Data are sent in JSON.

Arduino Code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char* ssid = "*******";
const char* password = "*******";

  
//Your Domain name with URL path or IP address with path
const char* serverName = "https://********.azurewebsites.net/api/Measurements/";

unsigned long lastTime = 0;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {

 
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED)
    {
      std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
      client->setInsecure();
     
      HTTPClient https;

      https.addHeader("Content-Type", "application/json"); // tried even "text/json"
      //https.addHeader("Content-Length", "32"); 
      https.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
      https.addHeader("Host","******.azurewebsites.net");  
      https.addHeader("Cache-Control","no-cache");  
     
      String httpRequestData = "[{\"SensorId\": 1, \"Value\": 77.7}]"; 
      
      if (https.begin(*client, serverName)) 
      {
          Serial.print("[HTTPS] POST...\n");
          int httpCode = https.POST(httpRequestData);

          if (httpCode > 0) 
          {
            // HTTP header has been send and Server response header has been handled
            Serial.printf("[HTTPS] POST... code: %d\n", httpCode);

            // file found at server
            if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) 
            {
              String payload = https.getString();
              Serial.println(payload);
            }
            else
            {
              Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
            }
          }
          else 
          {
              Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
          }
          
          https.end();
    }
    else 
    {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
   }
    lastTime = millis();
  }
}

Not receiving any error, just response code 415.

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload

JSON text:

[{"SensorId": 1, "Value": 77.7}]

AppInsight: enter image description here

PostMan successful request: enter image description here body of request in Postman:

enter image description here

And report from AppInsight:

enter image description here

App service:

enter image description here

Over PostMan I am able to reach azure and send data, but over Arduino I am not able to send data (I am able to get data, but not post)

Probably some beginner mistake. Can someone give me hint? Do I have wrong format of JSON?

2
  • 1
    Your code looks ok to me. I suggest you log your request in your azure app. From there you can see what is different between arduino and postman requests. Something like stackoverflow.com/a/67510839/5964792 Commented Sep 7, 2021 at 22:41
  • 1
    What if you use this: https.addHeader("Accept", "application/json");. You are are posting JSON, I would assume you should accept it as well. I don't see that in your request. Commented Sep 7, 2021 at 23:10

0

Your Answer

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.