-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.cpp
61 lines (57 loc) · 1.49 KB
/
http_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* ThePirat 2023 - Http client helper */
#include <WiFiClientSecure.h>
#include "http_client.h"
#include <HTTPClient.h>
HttpResponse HttpGet(const String& url, bool followRedirects)
{
HttpResponse response;
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure();
String payload;
{
HTTPClient https;
https.setFollowRedirects(followRedirects ? HTTPC_FORCE_FOLLOW_REDIRECTS : HTTPC_DISABLE_FOLLOW_REDIRECTS);
if (https.begin(*client, url))
{
response.status = https.GET();
response.body = https.getString();
https.end();
}
else
{
response.status = -1;
}
}
delete client;
return response;
}
HttpResponse HttpPost(const String& url, const String& payload, bool followRedirects)
{
HttpResponse response;
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure();
{
HTTPClient https;
https.setFollowRedirects(followRedirects ? HTTPC_FORCE_FOLLOW_REDIRECTS : HTTPC_DISABLE_FOLLOW_REDIRECTS);
https.addHeader("Content-Length", String(payload.length()));
https.addHeader("Content-Type", "application/json");
if (https.begin(*client, url))
{
response.status = https.POST(payload);
response.body = https.getString();
response.location = https.getLocation();
https.end();
}
else
{
response.status = -1;
}
}
delete client;
return response;
}
String GetPublicIp()
{
HttpResponse result = HttpGet("https://api.ipify.org/");
return result.body;
}