0

I'm kinda new to all this, so tell me if I'm doing something totally wrong.

I'm doing some gpio stuff with my raspberry pi, and at the moment i'm making something so the gpio pins can be controlled via a web interface. One of the ways I'm doing this is using bash CGI scripts to control the pins, and executing them from the browser.

So far the only way I can get this to work involves the browser loading the page ".../cgi-bin/gpio1.cgi" etc, which contains the code:

#!/bin/bash
echo "Content-type: text/html"
echo ""
...gpio stuff...

This works, but the browser navigates to the blank page created by this script.

Is there a way of executing these scripts without leaving the webpage, and so the scripts aren't writing HTML but instead focusing on the actual gpio stuff?

Thanks

1 Answer 1

1

Try this:

#!/bin/bash
echo "Status: 204 No Content"
...gpio stuff...

HTTP responses must start with a status line; webservers will normally add status "200 Ok" if the CGI doesn't specify one. That status must be accompanied by response body, which will form the new web page.

The status you want is 204, which indicates that the request was satisfied but that there is no response and the browser should stay on the same page. Normally, this would be a response to a POST request, not a GET request, but it should work anyway. Since a 204 response does not require a response body (in fact, it doesn't permit one), it should not be necessary to output a blank line following the status line, but you might need one if the script takes a long time to run.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but how do I run this from HTML? Navigating directly to the script in the browser (i.e. typing .../cgi-bin/script.cgi) leads to a 500 server error, so how else should I do it? Thanks
Sorry I should have said, the script RUNS perfectly, but I'm accessing it with a href, and it brings the browser to a 500 internal server error page
@ConorTaylor: Sorry, I didn't realize you were using a real webserver :) I edited the answer to take that into account, although I don't know which webserver you're using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.