Http headers dùng để làm gì, nó có quan trọng không? Và có những trường nào phải lưu ý
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Http headers là một phần rất quan trọng trong giao thức HTTP. Giờ chúng ta hãy xem tại sao nhé.
Một request của http sẽ có dạng:
Một response http có dạng:
Chi tiết em có thể tham khảo tại đây: https://datatracker.ietf.org/doc/html/rfc7232
HTTP Headers là nơi chúng ta set các metadata cho request và response, cũng là nơi chúng ta set các thông tin quan trọng như token chẳng hạn, và headers sẽ không bị hiển thị trên thanh công cụ của trình duyệt. Ví dụ:
https://login.com?token=123456
token sẽ bị hiển thị trên thanh công cụ, nhưng khi đưa vào header thì nó sẽ được ẩn đi chỉ còn https://login.com mà thôi.Các header quan trọng bao gồm:
1. Content-Type: để client server biết mà dùng các converter phù hợp, ví dụ application/json, sẽ nói cho server, client dùng các engine json để convert ra đối tượng
2. Các header liên quan đến Cookie: Trường này client sẽ tự thêm vào request và server cũng set ở response, em có thể lưu token ở đây để không phải set đi set lại qua query param.
3. Access-Control-Allow-Origin: Dùng cho config cors: https://tvd12.com/cau-hinh-cors-domain/
4. Location: dùng cho redirection