HTTP Headers: Accept and Content Type

A free video tutorial from Sergey Kargopolov
Software developer
Rating: 4.6 out of 5Instructor rating
11 courses
185,739 students
HTTP Headers: Accept and Content Type

Lecture description

In this video we will learn about Accept and Content type HTTP headers and how they used with RESTful Web Services. 

Learn more from the full course

RESTful Web Service with Java JAX-RS. Create & Deploy to AWS

* Create REST API with JAX-RS and Jersey * Build Restful Web Service * Deploy to AWS * Implement CRUD with Hibernate *

09:17:43 of on-demand video • Updated November 2018

Install Java, Netbeans IDE, Postman, MySQL Service and Apache Tomcat on local Mac computer
Learn to send HTTP Requests with Postman client
Use Netbeans to write Java code
Create Java Project with Maven
Configure Hibernate framework to persist Java objects in MySQL database
Build RESTful Java Web Services with JAX-RS and Jersey framework
Startup AWS EC2 Linux Server in Amazon Cloud
Configure AWS EC2 Instance Firewall
Connect to AWS EC2 Instance via SSH
Create MySQL database and users
Install MySQL Server and Apache Tomcat on AWS EC2 Linux Server
Deploy RESTful Java Web Service to a remote AWS EC2 Linux Server
Implement Email Verification with AWS SES(Simple Email Service)
English [Auto]
When we send the Http request to create a user profile, for example, the user details that are being sent are included in Http request body, and this information about the user profile is well structured in a Json format. So when server side script receives this information, it should be able to take this Json text and convert it into a Java object and for server side script to be able to convert this text into a Java object, it needs to know how to convert or it needs to know what type of content this Http request carries in its body. Is it XML format or is it Json format or is it some other format? Is it plain text? So additionally to structuring the user details into Json format, we need to tell or we need to include into Http request the details about the type of the content it is carrying. And to do that we can use Content-type Http header and we have a Tab headers where we can list all the headers that we need in this Http request. And the first one is called content type. So if I start typing here application, I can see all the different content types that Http header can carry, and the one that we need is application Json, application forward slash Json application Json because our body is in Json format. Okay. So when server side web service responds back to our request, it can also send us some additional information like for example, the user profile details or a list of all users. If we send the request to get a list of all users. And this information can also be structured in a different format. For example, it can be Json or it can be XML. So to tell server side which content type we expect back and we can accept we use a different Http header and it's called accept and we'll also provide application Json as the content type that we can accept. So let me now send this Http request which is going to use Http method post and then it will be sent to an endpoint API users. It's a collection of users and because it is an Http post request, a script that creates user profile will be triggered and it will accept this Json document as a material to create a new user profile. So let's send it and we get back a successful response which is also in Json format. So let me go to headers now and let me change the accept Http header from Json to XML, which will tell our web service that we accept back application XML rather than Json. So let me send it now and I get back a response which contains user details and some additional information like operation and the status of the response. But this information is structured in XML. Now, this conversion from Json into a Java object and from Java object into XML format takes place for us automatically. But unless we configure our web service to support one of these formats or both of these formats, this conversion will not take place. And in fact, if we attempt to send data in a format which our web service does not support or return data in a format that is not supported by a sender application, an error will take place and a requested operation might not even be completed successfully. So keep in mind these two Http headers and in my following videos I will show you how to configure web service to support each of these formats.