Hi, I am trying to send a GET request using a photon TCP client to a photon SoftAP HTTP server, given the client is connected to the server’s SoftAP. I could perform the GET request successfully using my browser, but I can’t get the request to work using the photon client. I will post snippets of my client code and server code to give more context (the server code is my edited version of @ScruffR’s code from another topic):
Client:
// already established wifi connection with server's SoftAP
void loop() {
while (!client.connect(server_ip, 80));
// Send HTTP data
client.println("GET /index.html?str1=1 HTTP/1.1");
client.println("Host: 192.168.0.1");
client.println();
Serial.println("GET request sent");
receivedData = "";
while (client.read() == -1); // the code stops executing from here since the request is never received by the server and no data is returned
Serial.println("data received");
client.read(recDataBuffer, sizeof(recDataBuffer));
// Print the string
Serial.println(receivedData);
client.stop();
delay(10000);
}
I personally don’t think the issue lies on the server side, since the browser has no trouble reaching the site. Here it is anyways.
Server:
struct Page
{
const char* url;
const char* mime_type;
const char* data;
};
Page myPages[] = {
{ "/index.html", "text/html", index_html },
{ nullptr }
};
void myPage(const char* url, ResponseCallback* cb, void* cbArg, Reader* body, Writer* result, void* reserved)
{
char _url[strlen(url)+1];
char* query;
char* value;
Log.trace("handling page %s", url);
strcpy(_url, url);
if (strtok_r(_url, "?&", &query))
{ // is it the control page [0]?
Log.trace("pre for() - URL: <%s>, *query: <%s>", _url, query);
while(query)
{ // and has it got a query string
Log.trace("in for() - URL: <%s>, *query: <%s>", _url, query);
char *token = strtok_r(query, "=", &value);
Log.trace("*query: <%s>, *value: <%s>, *token: <%s>", query, value, token);
if(strcmp(token, "str1") == 0)
{ // with a key str1
char* val = strtok_r(NULL, "?&=", &value);
Log.trace("String1: <%s>", val);
// logging in Serial
Serial.println("Webpage requested.");
}
query = value; // continue with the rest
}
}
if (strcmp(url,"/index")==0) {
Log.trace("sending redirect");
Header h("Location: /index.html\r\n");
cb(cbArg, 0, 301, "text/plain", &h);
return;
}
int8_t idx = 0;
for (;;idx++) {
Page& p = myPages[idx];
if (!p.url) {
idx = -1;
break;
}
else if (strcmp(_url, p.url)==0) {
break;
}
}
Log.trace("refresh page%d <%s>", idx, myPages[idx].url);
if (idx==-1) {
cb(cbArg, 0, 404, nullptr, nullptr);
}
else {
cb(cbArg, 0, 200, myPages[idx].mime_type, nullptr);
result->write(myPages[idx].data);
}
}