jpnyc
November 13, 2021, 8:10pm
1
Im very new to Arduino. Using an ESP32, Im trying to login to a network device to read its log file. To login to the device I need to simulate a web browser with cookies.
The ESP32 is connecting to WiFI correctly, and Im including the HTTPClient.h file
Using http.begin I am able to browse to the device login page, but I dont know how to store the successful login in a cookie.
I can successfully do it in PEARL using CURL (my code is below). Can someone help me port this to ESP32
use strict qw/refs/;
use HTTP::Cookies;
use LWP::UserAgent;
use HTTP::Request::Common qw/POST GET/;
my $Browser = LWP::UserAgent->new(
cookie_jar => HTTP::Cookies->new,
requests_redirectable => [],
timeout => 10,
pragma => "no-cache",
max-age => 0,
agent => "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0"
);
my $Page = $Browser->request(GET $Authenticate);
Juraj
November 14, 2021, 6:49am
2
there is no special support for cookie on esp32, but cookie is just a http header
Cookies are set using the Set-Cookie header field, sent in an HTTP response from the web server. This header field instructs the web browser to store the cookie and send it back in future requests to the server (the browser will ignore this header field if it does not support cookies or has disabled cookies).
As an example, the browser sends its first HTTP request for the homepage of the www.example.org website:
The server responds with two Set-Cookie header fields:
The server's HTTP response ...
noiasca
November 14, 2021, 1:43pm
3
you should collect the header field in the first response and reuse them in the next request
there are two methods for that:
void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
/// Response handling
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount);
I've never done this on a client, but this might give a first idea
const char* headers[] = {"Set-Cookie"};
client.collectHeaders(headers, sizeof(headers)/ sizeof(headers[0]));
parse the content and extract the information from headers.
add the information based on the wikipedia example to the next request:
client.addHeader("Cookie", "content");
system
Closed
May 13, 2022, 1:43pm
4
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.