-2

I need a Raspberry Pi - which I am coding in C - to be able to communicate with an HTTP server, similar to how a dynamic web page can use AJAX to interact with its origin server.

How do I do that? Do I call system() with a URL as parameter?

Or it there some other way to do it?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 1
    Do you mean a HTTP request? AJAX is javascript specific – Artyer Jan 27 '19 at 18:24
  • Isn’t *ajax* term related with javascript ecosystem? In C language there must be some other terms to refer communication with outside, imho. – ibubi Jan 27 '19 at 18:25
  • AJAX is a *programming technique* supporting dynamic web pages, involving asynchronously communicating XML messages with the origin server under control of Javascript. I've read that Google, one of the pioneers in this area, disfavors the term "AJAX", preferring instead to just say "Javascript". A C program can behave similarly, but it's unclear what degree of similarity you require, or how the the web-application concepts around which the technique is built map to your C program and its environment. – John Bollinger Jan 27 '19 at 18:37
  • I want my C prohram to do what a JS program would normally do - send an HTTP request and get a reply. – Mawg says reinstate Monica Jan 27 '19 at 19:09
  • 1
    The "J" is not the only distinction between AJAX and what you now seem to want, nor even the most important one. There are *numerous* ways for C programs to make requests to HTTP servers and receive their responses, and most of them have very few of the characteristics of AJAX. I have edited the question to further clarify. Even so, the question is still extremely broad. – John Bollinger Jan 27 '19 at 19:34
  • Thanks for the help, John. Can you please post an answer or comment with one of those numerous ways? Preferably the simplest. HTTP GET will do. In fact, I just realized that I don't even need a reply in this case, not even to check the return code. – Mawg says reinstate Monica Jan 28 '19 at 07:25
  • I ought to have used a different search term - https://stackoverflow.com/questions/11208299/how-to-make-an-http-get-request-in-c-without-libcurl – Mawg says reinstate Monica Jan 28 '19 at 08:22

1 Answers1

1

The very simplest way would be to use a system() call with curl. For example:

#include <stdlib.h>

int main() {
    system("curl http://example.com/");
}

This will send a GET request to example.com and print the output. Depending on your application, that might be enough. (See the manpage for more options.)

However, actually capturing the output and doing error handling will be more difficult than if you'd used a C library like curl.

Here's an example of how to use curl-the-library: https://curl.haxx.se/libcurl/c/simple.html

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • Yes, you can do that, but I wouldn't equate it with AJAX. You *could* do something AJAX-like with `libcurl`, but there's more to AJAX than just issuing requests and receiving responses. – John Bollinger Jan 27 '19 at 18:39
  • 2
    @JohnBollinger Sorry for answering the question instead of arguing with the asker. – Nick ODell Jan 27 '19 at 18:41
  • I don't see how you can be confident that you *have* answered the question. I'm inclined to think you haven't, but the question is not clear enough to be sure. – John Bollinger Jan 27 '19 at 18:43
  • That works for me. It's what I expected to have to do (I have updated the question). Of course, I also want to read the reply, but know how to do that. @Nick, do I have to use curl? I thought that was a PHP thing, but don't have experience of it. Btw, I upvoted, to cancel the downvote – Mawg says reinstate Monica Jan 27 '19 at 19:11
  • https://stackoverflow.com/questions/11208299/how-to-make-an-http-get-request-in-c-without-libcurl – Mawg says reinstate Monica Jan 28 '19 at 08:22
  • 1
    @Mawg You don't *have* to use curl. The HTTP protocol is pretty simple, and if you're just making a GET request to a single URL, you can avoid using any libraries. (As question you found shows.) If you want to make a POST request, or use cookies, or HTTPS, that's complicated enough that I'd use a library (such as curl) to do it. `I thought that was a PHP thing` It is available in PHP, but it's not just a PHP thing. – Nick ODell Jan 28 '19 at 18:44
  • Thanks (+1) It's all slowly coming back to me - sockets. I have only ever used raw sockets and sent datastreams, but figuring out `HTTP GET` shouldn't be too difficult. – Mawg says reinstate Monica Jan 28 '19 at 20:49