DeveloperBreeze

Simple Server-Side Handling of HTTP Methods

// index.php
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
    // Handle GET request
    echo json_encode($data);
} elseif ($method === 'POST') {
    // Handle POST request
    $data = json_decode(file_get_contents('php://input'), true);
    echo json_encode(['message' => 'Data received.']);
}
// Add similar blocks for other HTTP methods

Related Posts

More content you might like

Tutorial
javascript

AJAX with JavaScript: A Practical Guide

In this guide, we'll explore how AJAX works, the basics of making AJAX requests using vanilla JavaScript, and some real-world examples to help you implement AJAX in your own projects.

  • Basic knowledge of HTML, CSS, and JavaScript.
  • Familiarity with JSON (JavaScript Object Notation) as a format for exchanging data.

Sep 18, 2024
Read More
Tutorial
javascript

Getting Started with Axios in JavaScript

axios.get('https://jsonplaceholder.typicode.com/invalid-url')
  .then(response => {
    console.log('Data:', response.data);
  })
  .catch(error => {
    if (error.response) {
      console.error('Error Response:', error.response.data);
    } else if (error.request) {
      console.error('No Response:', error.request);
    } else {
      console.error('Error:', error.message);
    }
  });

If you're making multiple requests to the same base URL or with the same configuration, you can create an Axios instance.

Sep 02, 2024
Read More
Cheatsheet

REST API Cheatsheet: Comprehensive Guide with Examples

Introduction

REST (Representational State Transfer) is an architectural style for designing networked applications. It relies on a stateless, client-server communication protocol, usually HTTP. RESTful APIs are widely used due to their simplicity and scalability. This comprehensive cheatsheet covers essential REST API principles and operations, complete with examples presented in HTML tables for easy reference.

Aug 24, 2024
Read More
Tutorial
go

Building a RESTful API with Go and Gorilla Mux

func deleteBook(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	params := mux.Vars(r)
	for index, item := range books {
		if item.ID == params["id"] {
			books = append(books[:index], books[index+1:]...)
			break
		}
	}
	json.NewEncoder(w).Encode(books)
}

Start the server by running the following command in your terminal:

Aug 12, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!