Trying to do a cURL equivalent on ESP32 using HTTPClient - Not Yet Working

I've tried a variety of approaches using code similar to this

	WiFiClient Client;
	HTTPClient Http;
	int ResponseCode;
	String Url, ReqBody, ResponseStr;
	Url = "http://192.168.x.xxx:8080";
	ReqBody = "Action=Ping";
	Http.begin(Client, Url.c_str());
	/* Also tested
		Http.begin(Url.c_str());
		Http.begin(Url);
		Http.begin(Client, Url, Port); //i.e. separating the port from the URL
	*/
	Http.addHeader("Content-Type", "application/x-www-form-urlencoded");
	ResponseCode = Http.POST(ReqBody);
	/* Also tested
		ResponseCode = Http.GET();
		ResponseCode = Http.PUT(ReqBody);
	*/
	ResponseStr = Http.getString();
	Serial.println("ResponseCode: " + String(ResponseCode));
	Serial.println("ResponseStr: " + ResponseStr);
	Http.end();

The result is always either response code 400 or -1

The server at the above LAN address is Node.js designed for this kind of testing.
- It responds to all requests using any method GET, POST, PUT, DELETE, etc.
- It responds to any request header, body, any path -- really everything

Yet that Node server gives no indication of having received anything when the above code is run from an ESP32.

The ESP32 is confirmed connected to the same LAN

try to add / at the end of the url

1 Like

@Juraj RE "try to add / at the end of the url"

What a tiny detail. Totally did the trick. Thank you Juraj! What on earth clued you into that as the solution to this particular issue?

Been trying to crack this for 2 days.

There's a little more to the story. The node server is on a MacOS machine. If I use the friendly name (alias) to that Mac , e.g. 'NameOfMachine.local:Port/', it does NOT work. But specifying the LAN IP itself, e.g. 192.168.xx.xx/, then Juraj's suggestion makes all the difference!

Thanks again!!!

the HTTPClient parses the response into protocol, server, port and path. then uses path in GET request. without the / there is no path.

1 Like

@Juraj Very helpful. Thanks again.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.