ArduinoHttpClient.h two Strings

Hi there

String get_text = "/cgi-bin/sms_send?text=Hallo";
http_client_.get(get_text);

works.

String get_text = "/cgi-bin/sms_send?text=";
String get_text2 = "Hallo";
String get_complete = get_text + get_text2;
http_client_.get(get_complete);

Does not.

The server answer with bad request or a timeout is occuring.

I have the feeling there is a simple answer but i can't see it right now.
Any help appreciated, thank you.

PS: I also tried char* and copy the get request with sprintf, same strange behaviour.

I do not see anything wrong either. Can you share the entire sketch?

It's 8000 lines.. but i can embed it in the example:


#include <ArduinoHttpClient.h>
#include <WiFi101.h>

#include "arduino_secrets.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// WiFi Settings ///////
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

char serverAddress[] = "192.168.0.3";  // server address
int port = 8080;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(9600);
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  Serial.println("making GET request");
  String get_text = "/cgi-bin/sms_send?text=";
String get_text2 = "Hallo";
String get_complete = get_text + get_text2;
client.get(get_complete);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);
  Serial.println("Wait five seconds");
  delay(5000);
}

I can't imagine how the change you described could cause an error. I think it must be something else, a coincidence, making it look like that change caused the error.

Can you expand your example sketch to do the request both ways in turn, printing the string to serial monitor that is given to .get(), and post the text from serial monitor?

Oouuhhch maaaan, i found it out, i had a blank(space) in my second String without encoding...

Because i wrote a function and took the second string from the function parameter i didn't saw it because i never had a response from the server....

So i wrote

void RUT_SMS(String &RUT_SMS){ function }

and call it with RUT_SMS(String_variable); but the String_variable was "text 1" instead of "text%201" or whatever encoded is correct.

BTW: Does someone knows a good encoding function for GET requests?

Thanks anyway

And here's the remedy:

So why did this not work?

Because i had a unencoded value stored in my variable, not like i wrote in the example,
the error had nothing to do with the String operation. The response was a http 400 bad request.
My fault, i forgot the encoding and thought it was the String operation (You know, the C+ evil beast)

Correct is:

I think what you are saying is that the code you posted, that you said did not work, did in fact work. You assumed it would not work, but had not tested it? But you posted it here and asked the forum why it did not work.

The posted code here worked. But when the Variable get_text2 is filled with a String containing blanks, ä, ü etc. it does not. It's neccesary to encode the String before use it for a http method get.

I posted the explanation and the solution if somone later finds this post.

On my side, i tested the code within a large sketch and this issue came up and i had no idea why and that's why I allowed myself to consult the forum.

jfjlaros asked for a complete sketch thats why i pasted my code in the arduino example. Because i didn't know the type of error i posted accidentally a working sketch.

If i had known then, what i know now, i would not have asked the question...

Thanks again for your help

Ergo: For those who find this topic with the same issue : urlencode your http get / post.

Let me give some more important advice to those who find this topic:

Please don't post code you have not tested and claim that it does not work, because that would waste forum member's time.

If you find that you have wasted forum member's time, you could say sorry. We are all human and make mistakes sometimes, you will be forgiven.

Sorry PaulRB for wasting your time.

1 Like

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