pretty useless at code but I can but try.
I'm trying to send a HTTP request [GET] via localhost to a radio sever program. [Windows C++]
I can send this from my browser or cmd and the result is volume is set to zero
http://127.0.0.1:8282/parameter/hd1/spk/volume?set=0
I can connect with my c++ app, tried both winsock & libcURL. But the volume does not change
this is pretty much the example from cURL, I'm trying the smallest incremental steps I can.
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL* curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1:8282");
curl_easy_setopt(curl, CURLOPT_HTTPGET, "/parameter/hd1/spk/volume?=0");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
EDIT
This is ridiculous, but this did it, removed the CURLOPT_HTTPGET bit, and put the full path in CURLOPT_URL,
it's not supposed to work like that is it?
curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1:8282/parameter/hd1/spk/volume?=0");
Related
I'm trying to make an HTTP request with an ESP8266 and the ESP8266HTTPClient library. I have a request in the loop() function that makes a request every 5 seconds which works 100% flawlessly. However, I also have an interrupt setup like so:
void interrupt() {
if(WiFiMulti.run() == WL_CONNECTED) {
Serial.println("Knock!");
HTTPClient http;
knockhttp.begin(http_address + "/knock");
int httpCode = http.GET();
if(httpCode > 0) {
Serial.println(http.getString());
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
}
This fails. I get a "connection refused" Error every time the interrupt triggers. It's connecting to the same server as the request in the loop, just a different path.
Okay, I fixed it.
For some reason, HTTP requests with the ESP8266HTTPClient library do not work within interrupts, so here's the solution I came up with that worked:
Set some boolean flag in the interrupt.
Check that flag in the main loop
Perform the HTTP request in the main loop when the flag matches
Reset the flag.
Hope this helps anyone else that comes across the same strange problem.
I am using libcurl to send a POST request, and am trying to get response using the callback function. Below is the relevant code.
main ()
{
...
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init ();
curl_easy_setopt(curl, CURLOPT_URL, url_string);
curl_easy_setopt(curl, CURLOPT_POST, 1);
if (strlen(query_string) > 0)
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_buffer);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCB);
CURLcode res = curl_easy_perform(curl);
if (CURLE_OK == res)
printf("response: %s\n", write_buffer.data);
else
printf("curl failed\n");
curl_easy_cleanup(curl);
curl_global_cleanup();
...
}
struct BufferType
{
Str data;
BufferType() {};
size_t Append(char *src, size_t size, size_t nmemb)
{
data.Append(Str(src, size * nmemb));
return size * nmemb;
}
};
size_t WriteCB(char *data, size_t size, size_t nmemb, BufferType *buffer)
{
printf("WriteCB: %s\n", data);
fflush(stdout);
return buffer->Append(data, size, nmemb);
}
When I launched the program, I can see it is executed (the server responds with "200 OK"). But the program just hangs there, here is the output:
WriteCB: HTTP/1.1 100 Continue
WriteCB:
More info: if I use GET method for other URL, and change the two lines related to POST to
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
Then the code works fine.
What could be wrong?
SOLVED
In the command line, I let the user to specify query string, and I have a statement such that if the "query_string" is empty then do not call
"curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query_string);".
Setting curl to verbose shows that the request header has "Expect: 100-continue". So I guess it is because the query string is not set yet. Even it is empty, it should be set.
Glad you found the issue. I originally though you might not be using POST correctly with libcurl which is easy to do. So, I tried your code but didn't find any issue with it.
So, it had to be either an issue with the how you were setting it up or the server itself. The callbacks were behaving as expected. I wanted to try it out because I generally use POST with libcurl like this:
struct curl_httppost* post = NULL;
struct curl_httppost* last = NULL;
curl_formadd(&post, &last, ..., CURLFORM_END);
Here is an example
Please move/close this if the question isn't relevant.
Core: Cortex-M4
Microprocessor: TI TM4C1294NCPDT.
IP Stack: lwIP 1.4.1
I am using this microprocessor to do some data logging, and I want to send some information to a separate web server via a HTTP request in the form of:
http://123.456.789.012:8800/process.php?data1=foo&data2=bar&time=1234568789
and I want the processor to be able to see the response header (i.e if it was 200 OK or something went wrong) - it does not have to do display/recieve the actual content.
lwIP has a http server for the microprocessor, but I'm after the opposite (microprocessor is the client).
I am not sure how packets correlate to request/response headers, so I'm not sure how I'm meant to actually send/recieve information.
This ended up being pretty simple to implement, forgot to update this question.
I pretty much followed the instructions given on this site, which is the Raw/TCP 'documentation'.
Basically, The HTTP request is encoded in TCP packets, so to send data to my PHP server, I sent an HTTP request using TCP packets (lwIP does all the work).
The HTTP packet I want to send looks like this:
HEAD /process.php?data1=12&data2=5 HTTP/1.0
Host: mywebsite.com
To "translate" this to text which is understood by an HTTP server, you have to add "\r\n" carriage return/newline in your code. So it looks like this:
char *string = "HEAD /process.php?data1=12&data2=5 HTTP/1.0\r\nHost: mywebsite.com\r\n\r\n ";
Note that the end has two lots of "\r\n"
You can use GET or HEAD, but because I didn't care about HTML site my PHP server returned, I used HEAD (it returns a 200 OK on success, or a different code on failure).
The lwIP raw/tcp works on callbacks. You basically set up all the callback functions, then push the data you want to a TCP buffer (in this case, the TCP string specified above), and then you tell lwIP to send the packet.
Function to set up a TCP connection (this function is directly called by my application every time I want to send a TCP packet):
void tcp_setup(void)
{
uint32_t data = 0xdeadbeef;
/* create an ip */
struct ip_addr ip;
IP4_ADDR(&ip, 110,777,888,999); //IP of my PHP server
/* create the control block */
testpcb = tcp_new(); //testpcb is a global struct tcp_pcb
// as defined by lwIP
/* dummy data to pass to callbacks*/
tcp_arg(testpcb, &data);
/* register callbacks with the pcb */
tcp_err(testpcb, tcpErrorHandler);
tcp_recv(testpcb, tcpRecvCallback);
tcp_sent(testpcb, tcpSendCallback);
/* now connect */
tcp_connect(testpcb, &ip, 80, connectCallback);
}
Once a connection to my PHP server is established, the 'connectCallback' function is called by lwIP:
/* connection established callback, err is unused and only return 0 */
err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err)
{
UARTprintf("Connection Established.\n");
UARTprintf("Now sending a packet\n");
tcp_send_packet();
return 0;
}
This function calls the actual function tcp_send_packet() which sends the HTTP request, as follows:
uint32_t tcp_send_packet(void)
{
char *string = "HEAD /process.php?data1=12&data2=5 HTTP/1.0\r\nHost: mywebsite.com\r\n\r\n ";
uint32_t len = strlen(string);
/* push to buffer */
error = tcp_write(testpcb, string, strlen(string), TCP_WRITE_FLAG_COPY);
if (error) {
UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_write)\n", error);
return 1;
}
/* now send */
error = tcp_output(testpcb);
if (error) {
UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_output)\n", error);
return 1;
}
return 0;
}
Once the TCP packet has been sent (this is all need if you want to "hope for the best" and don't care if the data actually sent), the PHP server return a TCP packet (with a 200 OK, etc. and the HTML code if you used GET instead of HEAD). This code can be read and verified in the following code:
err_t tcpRecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
UARTprintf("Data recieved.\n");
if (p == NULL) {
UARTprintf("The remote host closed the connection.\n");
UARTprintf("Now I'm closing the connection.\n");
tcp_close_con();
return ERR_ABRT;
} else {
UARTprintf("Number of pbufs %d\n", pbuf_clen(p));
UARTprintf("Contents of pbuf %s\n", (char *)p->payload);
}
return 0;
}
p->payload contains the actual "200 OK", etc. information. Hopefully this helps someone.
I have left out some error checking in my code above to simplify the answer.
Take a look at the HTTP example in Wikipedia. The client will send the GET and HOST lines. The server will respond with many lines for a response. The first line will have the response code.
I managed to create an HTTP client for raspberry pi Pico W using the example here.
It uses the httpc_get_file or httpc_get_file_dns functions from the sdk.
However, that example is incomplete since it has a memory leak.
You will need to free the memory taken by the struct pbuf *hdr in the headers function and struct pbuf *p in the body function with respectively pbuf_free(hdr); and pbuf_free(p);
Without those modifications, it will stop working after about 20 calls (probably depends on the size of the response).
I'm currently working with an Arduino trying to build an ad hoc network to which a device can connect to and send web requests to. The problem I am currently having is that I can only set up one connection and then when that connection is terminated (with client.stop()), all subsequent connections are not picked up by the server, even a cURL command just sits there spinning. The first connection I start when I reset the server works fine, and I am able to talk to the server; but after that, the Arduino can no longer find new clients (even though it's trying with the library given).
I`m using the SparkFun library for the WiFly shield cloned from GitHub, along with an Arduino Uno.
My current code is based off their default example 'WiFly_AdHoc_Example', but I had to remove a few things to get the network to start up which might be the cause of this problem.
Here is the .ino file that I am running.
#include <SPI.h>
#include <WiFly.h>
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial( 5, 4); //Part from example not used (see below)
WiFlyServer server(80); //Use telnet port instead, if debugging with telnet
void setup()
{
Serial.begin(9600);
//The code below is from the example, but when I run it the WiFly will hang
// on Wifly.begin(). Without it, the WiFly starts up fine.
//mySerial.begin(9600);
//WiFly.setUart(&mySerial); // Tell the WiFly library that we are not
// using the SPIUart
Serial.println("**************Starting WiFly**************");
// Enable Adhoc mod
WiFly.begin(true);
Serial.println("WiFly started, creating network.");
if (!WiFly.createAdHocNetwork("wifly"))
{
Serial.print("Failed to create ad hoc network.");
while (1)
{
// Hang on failure.
}
}
Serial.println("Network created");
Serial.print("IP: ");
Serial.println(WiFly.ip());
Serial.println("Starting Server...");
server.begin();
Serial.print("Server started, waiting for client.");
}
void loop()
{
delay(200);
WiFlyClient client = server.available();
if (client)
{
Serial.println("Client Found.");
// A string to store received commands
String current_command = "";
while (client.connected())
{
if (client.available())
{
//Gets a character from the sent request.
char c = client.read();
if (c=='#' || c=='\n') //End of extraneous output
{
current_command = "";
}
else if(c!= '\n')
{
current_command+=c;
}
if (current_command== "get")
{
// output the value of each analog input pin
for (int i = 0; i < 6; i++)
{
client.print("analog input ");
client.print(i);
client.print(" is ");
client.print(analogRead(i));
client.println("<br />");
}
}
else if(current_command== "hello")
{
client.println("Hello there, I'm still here.");
}
else if (current_command== "quit")
{
client.println("Goodbye...");
client.stop();
current_command == "";
break;
}
else if (current_command == "*OPEN*")
{
current_command == "";
}
}
}
// Give the web browser time to receive the data
delay(200);
// close the connection
client.stop();
}
}
This script is just a mini protocol I set up to test. Once connected with the wifly module you can send text such as "get" "hello" or "quit" and the wifly module should respond back.
Using Telnet I can successfully connect (the first time) and send commands to the Arduino including "quit" to terminate the connection (calls the client.stop() method). But when I try to reconnect though Telnet, it says the connection was successful, but on the Arduino it's still looping thinking the client is still false. What??
I know right, I'm getting mixed messages from Telnet vs Arduino. None of the commands work obviously since the Ardunio is still looping waiting for a client that evaluates to true. I'm going to take a look at WiFlyServer from the library I imported and see if I can dig up the problem, because somehow that server.available() method isn't finding new clients.
I am noticing a lot of TODO's in the library code....
So I found the reason for the problem. It was in the WiFlyServer.cpp file from the SparkFun library. The code that was causing the reconnect issue was in fact the server.availible() method. Right at the top of the method, there is a check:
// TODO: Ensure no active non-server client connection.
if (!WiFly.serverConnectionActive) {
activeClient._port = 0;
}
For some reason when I comment this out, I can connect and reconnect perfectly fine and everything works as it should. I will now dive into the library and see if I can fix this, I'm not exactly sure what this is doing, but it gets called when the server connection is not active and is somehow blocking subsequent connections. The problem with this solution is that the Arduino always thinks it has found a client since client and client.connected() evaluate to true even if one doesn't exist. Even client.available() evaluates to true right when the connection is terminated and the ghost "client" is found, but after that first run through the if-statement the ghost "client" is no longer available(). Even with this flaw it still picks up a new client when it comes along which is why it works.
How might I get to the root of this problem without using this commenting hack?
Are their any risks or future problems I might run into doing it this way?
What is the purpose of the block that I commented out in the first place?
Well, when you're calling client.stop(); how does the Arduino know whether the client has to start again?
Remember setup() executes only once.
Have you tried to include the following code in your loop to tell the Arduino to create the WiFly AdHoc network again? This may or may not work. I don't have one myself and haven't played with the Wifly shield but it's worth a try.
Remember to only ever execute the code once every time you need to connect again since it's sitting inside a loop that's always going to be running.
WiFly.begin(true);
Serial.println("WiFly started, creating network.");
if (!WiFly.createAdHocNetwork("wifly"))
{
Serial.print("Failed to create ad hoc network.");
while (1)
{
// Hang on failure.
}
}
I have a wxWidgets application and would like to add a way for users to submit feedback in a simple (implementation and usage), reliable, cross-platform and secure way. Using HTTP POST over SSL seems to fit those requirements best (although I'll consider answers that suggest other approaches). However, support for HTTPS in wxWidgets seems limited.
Here are some of the options I've considered and the problems with them:
wxSMTP: no SSL/TLS support that I've found. Relies on user having a correct mail configuration (sendmail, MAPI).
wxHTTP: everything but SSL/HTTPS support.
wxSSL: everything if it wasn't incomplete and long dead.
wxCURL: everything but complicated to build/incorporate (in fact currently release fails to build).
libcurl: just link with and call into libcurl directly. This is the solution I've settled on (and I have a working prototype) but it feels very non-wx and while libcurl is cross-platform, Windows is definitely not its native platform so it adds significant dependency and build complexity to the project.
I've decided to go with libCURL linked to openssl. Both of the packages are able availabe on most Linux system and can be fairly easily built on Windows and OS X.
Here is example C code that sends feedback:
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
/* Fill in the name field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "name",
CURLFORM_COPYCONTENTS, "John Doe",
CURLFORM_END);
/* Fill in the comments field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "comments",
CURLFORM_COPYCONTENTS, "using HTTPS POST\nline 2\nline 3",
CURLFORM_END);
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL,
"https://some_host.com/path/feedback.php");
// Uncomment to disable certificate checks
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
printf("Curl result: %d\n", res);
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
}
return 0;
}
That can be compiled on Linux like so:
gcc post.c -lcurl -o post
Here is example PHP code that accepts that post:
<?php
$recipient = "john#some_host.com";
if (empty($_POST)) {
// We only accpet POSTs
header('HTTP/1.0 403 Forbidden');
exit;
} else {
// Handle a POST
$message .= "Submitted at ".date("F j, Y, g:i a")."\n\n";
$message .= "Name:\n";
$message .= $_POST['name']."\n\n";
$message .= "-------------------------------------------\n\n";
$message .= "Comments:\n\n";
$message .= $_POST['comments']."\n\n";
// Send message to email address
$sent = mail($recipient, "Feedback",
$message, "From: Feedback <noreply#some_host.com>\r\n");
if ($sent) {
?>
<html>
<body>
Got POST and sent email:
<pre><? echo $message; ?></pre>
</body>
</html>
<?php
} else {
// Return an error
header('HTTP/1.0 500 Internal Server Error', true, 500);
exit;
}
}
?>
Boost::Asio is an alternative I've used before, even in a wxWidgets app. I've used it to download files before (and only have sample code for that), but sadly don't have (and can't quickly find) sample code for HTTP(S) POSTs.
Boost::asio has a "problem" in that it's Boost (which is either a big problem because "ugh, Boost", or not a problem "Awesome, Boost!"