简体   繁体   English

在 NodeJS 应用程序中发出 HTTP 请求时,何时显式设置标头

[英]When to set the headers explicitly when making HTTP request in NodeJS application

So I was having a look at a codebase of a NodeJS application and there were some specific functions making HTTP requests to the backend.因此,我查看了 NodeJS 应用程序的代码库,并且有一些特定的函数向后端发出 HTTP 请求。 To be exact, those functions were making a GET request to the backend and one thing that I found confusing was that in some of the functions, the headers were mentioned explicitly whereas, in some other functions who were making the GET request, there was no mention of headers (ie headers were not being set explicitly).确切地说,这些函数正在向后端发出 GET 请求,而我发现令人困惑的一件事是,在某些函数中,明确提到了标头,而在其他一些发出 GET 请求的函数中,没有提到标题(即标题没有被明确设置)。 Below is an example:下面是一个例子:

In the code below, the function is making a GET request and there's no mention of headers (ie the headers are not being set explicitly):在下面的代码中,function 正在发出 GET 请求,并且没有提及标头(即标头未明确设置):

// Method for fetching a single post from the backend on the basis of the post ID
export const singlePost = (postID) => {
  return fetch(http://localhost:8080/post/${postID}, {
    method: "GET",
  })
    .then((response) => {
      return response.json();
    })
    .catch((error) => {
      console.log(error);
    });
};

In the code below, the function is making a GET request and the headers are being set explicitly:在下面的代码中,function 正在发出 GET 请求,并且标头被明确设置:

// Helper Method for making the call to the backend and fetching all their details of all the posts
export const list = (page) => {
  return fetch(http://localhost:8080/posts/?page=${page}, {
    method: "GET",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
  })
    .then((response) => {
      return response.json();
    })
    .catch((error) => console.log(error));
};

Now coming to the main question, could someone please explain to me when are we supposed to set the headers explicitly not only in just GET request but in other general HTTP requests as well (ie POST, PUT, OPTION etc).现在来到主要问题,有人可以向我解释一下我们什么时候应该明确设置标头,不仅在 GET 请求中,而且在其他一般 HTTP 请求中(即 POST、PUT、OPTION 等)。

It would be really great if some could refer a source or explain this concept here.如果有人可以在这里参考来源或解释这个概念,那就太好了。 Thanks!谢谢!

HTTP request header is the information, in the form of a text record, that a user's browser sends to a Web server containing the details of what the browser wants and will accept back from the server. HTTP 请求 header 是用户的浏览器以文本记录的形式发送给 Web 服务器的信息,其中包含浏览器想要的详细信息,并将接受来自服务器的详细信息。 The request header also contains the type, version and capabilities of the browser that is making the request so that server returns compatible data.请求 header 还包含发出请求的浏览器的类型、版本和功能,以便服务器返回兼容数据。 Check this https://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039检查这个https://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039

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

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