Table of contents
1.
Introduction⭕
2.
Understanding XML✅
3.
What is Content Negotiation?🙄
3.1.
Types Of Content Negotiation😎
3.2.
Content Negotiation By HTTP Headers🎯
3.3.
Content Negotiation By URL Patterns🎯
4.
Adding Support For XML in Headers😎
5.
Frequently Asked Questions
5.1.
What is pom?
5.2.
Which HTTP header is used for content negotiation?
5.3.
What is a content-type?
5.4.
Which content type is set as default?
5.5.
Which is better, XML or JSON?
6.
Conclusion
Last Updated: Mar 27, 2024

Content Negotiation Implementing Support for XML

Author Akriti Bhan
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction⭕

This blog will discuss the basic concepts of content negotiation implementing support for XML. We will first learn about the XML format and the need for content negotiation implementing support for XML. The blog will further explore the types of content negotiation and the steps to add content negotiation implementing support for XML in your project.

content negotiation

Let us directly start exploring content negotiation implementing support for XML.🎯

Understanding XML✅

what is xml

Before moving directly to the content negotiation for XML, let us first understand what XML is. XML stands for eXtensible Markup Language. It is a tool for the storage and transportation of data. It is software and hardware independent in nature. After going through the term, markup language, one thing that would strike your mind is HTML. We are quite aware that HTML is also a markup language, then what is the difference between HTML and XML❓. 

HTML primarily focuses on the data being more visually appealing. XML focuses on the nature of the data and its transportation. The user creates the tags that are used in the XML. In HTML, the tags are already defined.

What is Content Negotiation?🙄

We will now discuss what exactly content negotiation is. Content negotiation basically refers to the process of selecting the best representation for a response. This becomes essential as there are many ways in which a resource can be represented. Different types of representations occur because different clients want different representations of the resource. Content negotiation is based on media type and the media type formatter. It is done on the server side in the web API. 

The client requests data in a particular format. Most of the time, the server fulfills this requirement. In some other cases, this request cannot be fulfilled. Hence, it is called negotiation, as the client and the server negotiate for the response's data format.

Types Of Content Negotiation😎

types

There are two types of content negotiation.

1️⃣Server-driven content negotiation

This is done at the end of the server. The server decides the format of the response. In server-driven content negotiation, the browser is responsible for sending HTTP headers with the URL of the resource. Various headers determine server-driven negotiation. These are defined in the HTTP/1.1. Some examples include Accept, Accept-Language, etc. However, everything has its pros and cons. The disadvantages of using server-driven negotiation are as follows:

📌The information requested from the client raises the issue of privacy.

📌The implementation of server negotiation is complex.

📌The caches that are shared are inefficient.

📌The server does not have complete knowledge of the browser. Hence its choice is sometimes dicey.

📌The number of headers increases the message size.

 

2️⃣Agent-driven content negotiation

In this, the agent selects the representation of the response. This lists out the available options when a specific response representation cannot be specified. The role of the server is to send a page containing these links. The user closely examines and thus chooses the best representation. It is also known as reactive negotiation.

Out of the two, primarily agent-driven content negotiation is used for REST API. These are done using HTTP requests.

The content negotiation implementing support for XML can be done via HTTP headers or URLs. Let us look at them.⭕

Content Negotiation By HTTP Headers🎯

In this type, the content negotiation is done based on the headers provided along with the HTTP in the request. The server determines the type of identity of the request by looking at the Content-Type. For example, content-type : application/xml. The client demands certain types of representation. This is determined by looking at the ACCEPT type in the header. For example, Accept: application/xml.

Content Negotiation By URL Patterns🎯

In this, the client uses a certain URL for the resource representation. It returns the response in XML format.

Adding Support For XML in Headers😎

xml in headers

To send a request, we need to specify the Key and Value parameter in the headers section. When we specify the key as Accept and value as application/xml it will return us Status: 406 not acceptable. This implies that xml is an invalid accept header.

invalid accept header

So, how we can start with content negotiation implementing support for XML❓Let us learn it🎯

1️⃣Go to pom.xml

2️⃣Add the jackson-dataformat-xml dependency.

3️⃣Restart the Postman.

4️⃣Open Postman (REST client).

5️⃣Now, send the GET request by specifying the application/xml in the header.

 

Let us now try to send a POST request using the xml in the header.🎯


1️⃣Open the headers tab.

headers tab

2️⃣Go to Content-Type.

3️⃣Add Content-Type:application/xml and Accept: application/xml.

4️⃣Go to the Body tab.

body tab

5️⃣Enter the required details.

6️⃣Select the XML type.

xml type

7️⃣Click on Send to send the request.

send

We hope you are now well versed with content negotiation implementing support for XML🎯

Frequently Asked Questions

What is pom?

Pom stands for Project Object Model. It is a fundamental unit for Maven. It basically is an XML file containing the information about the project.

Which HTTP header is used for content negotiation?

The Accept header is used for content negotiation. It tells about the representation that the client demands.

What is a content-type?

The content-type specifies the type of the data format in which it is sent or returned.

Which content type is set as default?

The default content type is application/octet-stream.

Which is better, XML or JSON?

JSON is comparatively easier to understand than XML. XML makes handling the data easier and decreases the software risk.

Conclusion

In this blog, we discussed the content negotiation implementing support for XML. We first started with a brief discussion on XML. The blog further explored content negotiation and its types. In the end, we finally discussed the ways to add support for XML in the header for GET and POST requests.

You can refer to other similar articles as well

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available.

Happy Learning Ninja! 🥷

Live masterclass