Hi,
I am trying to build a HTTP POST payload and as per the RFC I need to calculate the Content-Length and insert it into the HTTP Header.
The issue I have is when I try and insert an int into the Char is never seems to insert the value.
Here is my simplified code.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int bodyLength = 112;
//Serial.println(bodyLength);
char payload[330]; // verify this
// HTTP Header
char* HTTPHeader_Request = "POST /api/dataPoint/create.php HTTP/1.1\r\n";
char* HTTPHeader_ContentType = "Content-Type: application/json; charset=UTF-8\r\n";
char* HTTPHeader_UserAgent = "User-Agent: ArdunioIOT\r\n";
char* HTTPHeader_Accept = "Accept: */*\r\n";
char* HTTPHeader_CacheControl = "Cache-Control: no-cache\r\n";
char* HTTPHeader_Host = "Host: 192.168.1.196\r\n";
char* HTTPHeader_ContentLength = "Content-Length: ";
char* HTTPHeader_ContentLength_data = bodyLength; //value
char* HTTPHeader_Connection = "\r\nConnection: keep-alive\r\n\r\n";
strcpy(payload, HTTPHeader_Request);
strcpy(payload + strlen(payload), HTTPHeader_ContentType);
strcpy(payload + strlen(payload), HTTPHeader_UserAgent);
strcpy(payload + strlen(payload), HTTPHeader_Accept);
strcpy(payload + strlen(payload), HTTPHeader_CacheControl);
strcpy(payload + strlen(payload), HTTPHeader_Host);
strcpy(payload + strlen(payload), HTTPHeader_ContentLength);
strcpy(payload + strlen(payload), HTTPHeader_ContentLength_data);
strcpy(payload + strlen(payload), HTTPHeader_Connection);
Serial.println(payload);
while(1);
}
when I try and insert an int into the Char
Explain how you are trying to do this. If you want to convert an int variable into an ASCII numeric C-string, use the function itoa().
never seems to insert the value.
What happens instead? Post example output.
@Delta_G
The output on the console doesn't show the p which is what is confusing me. I get that 112 is ASCII lower p but why is the output "null"? As in it doesn't print any character. There is the space character from the previous line and then the \r\n from the line below.
See debug output below.
@jremington
Here is the serial output.
POST /api/dataPoint/create.php HTTP/1.1
Content-Type: application/json; charset=UTF-8
User-Agent: ArdunioIOT
Accept: */*
Cache-Control: no-cache
Host: 192.168.1.196
Content-Length:
Connection: keep-alive
(I have left the 2 line breaks at the end for completeness).
I will look at both itoa() and sprintf as well.
Thanks for the super fast replies.
Thanks Delta_G.
I figured it was something to do with the pointer. I am struggling to get my head around the difference between char and char* despite a few hours of Googling. If you have any resources I could read up on I would appreciate it.
Otherwise thanks and will keep on Googling!
system
July 17, 2019, 11:26pm
8
Here is some code for you to play with and examine. I hope it shows a bit more what people are talking about and helps you.
Sloppy code, but it is just to show you some basics.
Please run it, and examine what each step is.
char oneChar = 'a';
char arrayOfChar[] = "Arduino Coding!";
char *pointerToChar = arrayOfChar;
char *dynamicArrayOfchar;
int someNumber = 4321;
void setup()
{
Serial.begin(115200);
Serial.println(oneChar);
Serial.println(arrayOfChar);
Serial.println(pointerToChar);
dynamicArrayOfchar = new char[25];
strcpy(dynamicArrayOfchar,"Hello Fellow Programmer!");
Serial.println(dynamicArrayOfchar);
delete [] dynamicArrayOfchar;
char printSomenumber[20];
itoa(someNumber, printSomenumber, 10);
Serial.println(printSomenumber);
}
void loop()
{
// put your main code here, to run repeatedly:
}
Another simple example:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
char a[24] = "The number is: ";
char tmp[6];
int x = 1234;
Serial.println(strcat(a, itoa(x, tmp, 10))); //prints The number is: 1234
}