简体   繁体   English

这个HTTP请求如何工作?

[英]How does this HTTP request work?

To begin, this question deals primarily with HTTP requests, BackboneJS, some sort of RESTful API (such as Slim API), and how these things work with each other. 首先,这个问题主要涉及HTTP请求,BackboneJS,某种RESTful API(例如Slim API),以及这些内容如何相互协作。 Additionally, this question is coming from someone who doesn't have much experience on the server-side, other than just handling basic PHP/MySQL stuff. 此外,这个问题来自于在服务器端没有太多经验的人,除了处理基本的PHP / MySQL之外。

I've been looking at Backbone, and I've seen some tutorials regarding the use of RESTful APIs on the back-end (including this one from 9bit ). 我一直在看Backbone,我看过一些关于在后端使用RESTful API的教程(包括这个来自9bit的教程)。

I also read this answer to a StackOverflow question (Understand BackboneJS REST Calls). 我还阅读了StackOverflow问题的答案 (了解BackboneJS REST调用)。

If I open up a JS file, and type type in some code to send a POST request such as this: 如果我打开一个JS文件,并在某些代码中输入type来发送POST请求,例如:

(function() {
var http = new XMLHttpRequest();

var value = '{ "prop1": "value 1", "prop2": "value 2" }';

http.open('POST', 'dir', true);
http.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
http.setRequestHeader('Content-Length', value.length);
http.onreadystatechange = function () {
    if (http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(value); 
})();

I see in the console that it sent a POST request looking something like this: 我在控制台中看到它发送了一个POST请求,看起来像这样:

Method: POST
Body: { "prop1": "value 1", "prop2": "value 2" }
Location: http://localhost/~myusername/Todo/dir/

And then, since it's just my local server, the server sends back a response with the basic HTML page that shows the files in that directory, etc. 然后,由于它只是我的本地服务器,服务器会发回一个响应,其中包含显示该目录中文件的基本HTML页面等。

I tried using a GET request to retrieve a similar JSON object as well, but obviously I couldn't get anything from that location, presumably because the object I was trying to request from some empty folder doesn't even exist anywhere. 我尝试使用GET请求来检索类似的JSON对象,但显然我无法从该位置获取任何内容,可能是因为我试图从某个空文件夹请求的对象甚至不存在于任何地方。

My question is, when you use a BackboneJS method such as .save(), from what I understand, it might use, in that instance, a PUT method to send a request with a body of an object, perhaps parsed as a string, to a directory, such as 'article/id', with 'id' possibly being something like '43' (potentially the corresponding id of whatever model's properties you sent). 我的问题是,当你使用BacksJS方法,如.save()时,根据我的理解,在这种情况下,它可能会使用PUT方法来发送一个对象体的请求,可能被解析为一个字符串,到一个目录,例如'article / id','id'可能是'43'(可能是你发送的任何模型属性的相应id)。 So... 所以...

1) What does an API, such as Slim do with that request? 1)一个API,比如Slim对该请求做了什么?
2) Where is it saving those object properties to (a MySQL database)? 2)将这些对象属性保存到哪里(MySQL数据库)?
3) Is there a file, such as 'index.php', sitting in the directory 'article', in which a script grabs the parameters in the body of the POST requests and uses though to communicate with the MySQL database? 3)是否有一个文件,例如'index.php',位于目录'article'中,其中脚本抓取POST请求正文中的参数并使用它来与MySQL数据库通信? (I'm wondering why the location is simply a 'folder', such as '/article'. To put it in another context, whenever you type in a website like ' http://www.mywebsite.com ', the server will automatically look for an 'index' page in that directory, such as 'index.html', and automatically open that file as the default file of that directory. Is that same type of thing happening in the context of using a location '/somefoldername' as the location of the HTTP request)? (我想知道为什么这个位置只是一个'文件夹',比如'/ article'。要把它放在另一个上下文中,每当你输入像' http://www.mywebsite.com '这样的网站时 ,服务器将自动在该目录中查找“索引”页面,例如“index.html”,并自动将该文件作为该目录的默认文件打开。是否在使用位置的上下文中发生了相同类型的事件'/ somefoldername'作为HTTP请求的位置)?

Basically, it just looks strange to me that you would send an HTTP request to just some folder, and not a specific PHP file (for example) that would handle the request and communicate with a database. 基本上,我只是觉得你会向某个文件夹发送一个HTTP请求,而不是一个特定的PHP文件(例如)来处理请求并与数据库通信。 Using BackboneJS with a RESTful API, would our theoretical folder '/article' even exist, or is that just appended to the URL for some reason? 将BackboneJS与RESTful API一起使用,我们的理论文件夹'/ article'是否存在,或者由于某种原因只是附加到URL?

Thank you very much. 非常感谢你。

Since you asked your questions in the context of Slim, I'm going to answer them that way, although much of the information will generally apply to other web applications/frameworks. 由于您在Slim的上下文中提出了问题,我将以这种方式回答它们,尽管许多信息通常适用于其他Web应用程序/框架。

What does an API, such as Slim do with that request? 一个API,比如Slim对该请求做了什么?

Not to be to cheeky, but it does whatever you (or the API developer) wants it to do (more on that shortly). 不是为了厚颜无耻,但它会做任何你(或API开发人员)想要它做的事情(很快就会更多)。

Where is it saving those object properties to (a MySQL database)? 它将这些对象属性保存到哪里(MySQL数据库)?

In general, those object properties are being used to create a resource (POST) or to updated a resource (PUT), and most likely the resource is being persisted in some sort of storage, be it an RDMS or a NoSQL solution. 通常,这些对象属性用于创建资源(POST)或更新资源(PUT),并且很可能资源被保存在某种存储中,无论是RDMS还是NoSQL解决方案。

Is there a file, such as 'index.php', sitting in the directory 'article', in which a script grabs the parameters in the body of the POST requests and uses though to communicate with the MySQL database? 是否有一个文件,例如'index.php',位于目录'article'中,其中脚本抓取POST请求正文中的参数并使用它来与MySQL数据库通信?

Here's where things get interesting, IMHO. 这是事情变得有趣的地方,恕我直言。 Considering the route article/id , here's what happens in a Slim application: 考虑路径article/id ,这是Slim应用程序中发生的事情:

  1. A request is received at example.com/article/22 请求来自example.com/article/22
  2. The request is routed to a front controller script which, based on the request URI and HTTP method, makes a decision about what to do with the request. 请求被路由到前端控制器脚本,该脚本基于请求URI和HTTP方法,决定如何处理请求。
  3. If a PUT route for article exists, then the application code perhaps would take the request body and update the resource identified by the provided id. 如果文章的PUT路由存在,那么应用程序代码可能会接受请求主体并更新由提供的id标识的资源。

With that in mind, in a Slim application, likely the only web accessible file is index.php. 考虑到这一点,在Slim应用程序中,可能唯一的Web可访问文件是index.php。 What appear to be directories are merely routes defined in the application's index.php that Slim used to make decisions about how to handle requests. 似乎是目录的只是在应用程序的index.php中定义的路由,Slim用它来决定如何处理请求。 With that in mind . 考虑到这一点 。 . .

Using BackboneJS with a RESTful API, would our theoretical folder '/article' even exist, or is that just appended to the URL for some reason? 将BackboneJS与RESTful API一起使用,我们的理论文件夹'/ article'是否存在,或者由于某种原因只是附加到URL?

In the context of a Slim application, no, /article wouldn't exist as a directory, but rather a route. 在Slim应用程序的上下文中,不, /article不会作为目录存在,而是作为路由存在。

Perhaps this won't help much, but this is what a portion of that index.php routing file might look like on the Slim side: 也许这没有多大帮助,但这就是那个index.php路由文件的一部分可能在Slim方面看起来像:

$app->post('/article', function () {
    // Get data from post
    // Create resource
});

$app->get('/article/:id', function ($id) {
    // Return an article resource identified by $id
});

$app->put('/article/:id', function ($id) {
    // Use $id to retrieve resource from storage
    // Update resource with request data
});

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

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