I've search everywhere for this problem and still couldn't fix it. Can anyone help me please? write down your fix code
{"Message":"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource."}
Here's output:
connecting to sv.cpe.com.vn
Requesting URL: /api/values
HTTP/1.1 415 Unsupported Media Type
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 10 Jun 2020 16:07:13 GMT
Connection: close
Content-Length: 164
Here's my code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <base64.h>
String Username = "";
String Password = "";
char ssid[] = ""; //Wifi name
char pass[] = ""; //Wifi Password
int sensorPin = 16; // ket noi cam bien vs Pin4
int relayPin = 4;// ket noi button vs Pin3
int sensor_value=0;
int val1, val2, val3, val4;
String c1, c2, c3, c4;
byte moc;
String trang_thai="false";
const char* host="sv.cpe.com.vn";
void setup()
{
Serial.begin(115200);
delay(10); // We start by connecting to a WiFi network
Serial.println();
Serial.println(); Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
pinMode (sensorPin, INPUT); // cảm biến
pinMode (relayPin, OUTPUT); // đầu ra điều khiển
Serial1.begin(115200);
}
void loop() {
//đọc giá trị từ cảm biến
sensor_value = digitalRead(sensorPin);
Serial.print("GiaTriCamBien:");
Serial.println(sensor_value);
//nếu mà cảm biến ON thì mở relayPin on
String gtbor;
if(sensor_value==HIGH)
{
digitalWrite(relayPin, HIGH);Serial.println("ON");
gtbor="true";
}
else
{
digitalWrite(relayPin, LOW);Serial.println("OFF");
gtbor="false";
}
delay(1000);
//cấy đoạn code gửi lên api
HTTPClient http;
String postData;
http.begin("http://sv.cpe.com.vn/api/values/Post"); //
String auth = base64::encode(Username + ":" + Password);
http.addHeader("Authorization", "Basic " + auth);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
//Post Data
String tt="false";
if(sensor_value==1)
{
tt="true";
}
else
{
tt="false";
}
if((trang_thai!=tt)&&(tt=="true"))
{
postData = "id_machdien=333&trang_thai="+tt+"&giatri_bor="+gtbor;
int httpCode = http.POST(postData); //gửi giá trị lên api
String payload = http.getString(); //lấy phản hồi từ api
if (Serial.available()) {
payload = Serial.readString(); //Serial đọc chuỗi
}
c1 = payload;
Serial.println(c1);
http.end(); //đóng kết nối
delay(2000);
}
else
{
postData = "id_machdien=333&trang_thai="+tt+"&giatri_bor="+gtbor;
//Serial.println(postData);
int httpCode = http.POST(postData);
http.end();
delay(2000);
}
//////////////////////////////////////
WiFiClient client;
int value=0;
delay(5000); ++value; Serial.print("connecting to ");
//http.addHeader("Accept","application/json");
String auth1 = base64::encode(Username + ":" + Password);
http.addHeader("Authorization", "Basic " + auth1);
Serial.println(host);
client.println("Content-Type: application/json; charset=UTF-8");
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/api/values";
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000)
{ Serial.println(">>> Client Timeout !");
client.stop(); return; } }
while (client.available())
{ String line = client.readStringUntil('\r'); Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
//////////////////////////////////////
}