简体   繁体   English

使用REST时的HTTP请求

[英]HTTP Requests when using REST

I'm trying to write a REST API. 我正在尝试编写REST API。 I honestly do not know anything related to REST structure so I've done some research and find a valuable information that one of the key things that separates REST from other structures is that it utilizes the 'GET','POST','PUT','DELETE' requests of HTTP. 老实说,我不了解与REST结构相关的任何信息,因此我进行了一些研究,发现了有价值的信息,即将REST与其他结构区分开的关键因素之一是它利用了“ GET”,“ POST”,“ PUT” ,HTTP的“ DELETE”请求。

This might come out silly but how can i control the request type. 这可能很愚蠢,但我如何控制请求类型。 For example if I code it and simply enter a URL www.example.com/users/1234 , how will my server side analyze whether its a GET,POST,PUT or DELETE request. 例如,如果我对其进行编码并仅输入URL www.example.com/users/1234 ,我的服务器端将如何分析它是GET,POST,PUT还是DELETE请求。

Sorry if I'm asking an obvious question, but I would love have some information related to this subject 抱歉,如果我要提出一个明显的问题,但是我希望知道与此主题相关的一些信息

A good place to start is by looking at the SO wiki associated with the rest tag . 一个不错的起点是查看rest标签关联SO Wiki This is a part of SO that is so often overlooked as a great source of information. 这是SO的一部分,因此常常被视为重要的信息来源。 Just hover over the tag and select the "info" link. 只需将鼠标悬停在标签上,然后选择“信息”链接即可。

Since you mentioned LAMP, I post here, a minimal PHP script which is aware of the method being called: 由于您提到了LAMP,因此我在这里发布了一个最小的PHP脚本,该脚本知道被调用的方法:

<?php
  if($_SERVER['REQUEST_METHOD'] == "POST") { 
    echo "Got a POST Request";
  } else {
    echo "Got a non-POST Request";
  }
?>

Look at this document which describes a lot of 'standard' PHP variables containing information about and content from the request, and so on: 请看一下这份文档,其中描述了很多“标准” PHP变量,其中包含有关请求的信息和来自请求的内容,等等:

http://www.php.net/manual/en/reserved.variables.server.php http://www.php.net/manual/en/reserved.variables.server.php

If you install and run Apache with the PHP module enabled, and serve a PHP file like the above, requesting that file from a client will return the appropriate string. 如果在启用PHP模块的情况下安装并运行Apache,并提供上述PHP文件,则从客户端请求该文件将返回适当的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM