Posting XML with Correct Content-Type Header [C#/.NET Code]

To post XML to the server using C#/.NET, you need to make an HTTP POST request, include the XML data in the body of the POST request message, and set the correct MIME type for the XML using the "Content-Type: application/xml" HTML header. Optionally, you can send an "Accept: application/xml" request header that will tell the server that the client is expecting XML in response. The server specifies the data type in the response using the "Content-Type: application/xml" HTTP response header. In this C#/.NET Post XML Example, we send a request with XML data to the ReqBin echo URL and pass the Content-Type: application/xml HTML header. Click Send to execute the C#/.NET POST XML example online and see the results. The C#/.NET code was automatically generated for the POST XML example.
Posting XML with Correct Content-Type Header [C#/.NET Code] Send
POST /echo/post/xml HTTP/1.1
Host: reqbin.com
Content-Type: application/xml
Accept: application/xml
Content-Length: 118

<?xml version="1.0" encoding="utf-8"?>
<Request>
    <Login>login</Login>
    <Password>password</Password>
</Request>
Updated: Viewed: 61233 times
C#/.NET code for POST XML example

C#/.NET code for POST XML Example

This C#/.NET code snippet was generated automatically for the POST XML example.
<< Back to the POST XML example

What is XML?

XML is an eXtensible Markup Language. It was designed to be a language with a simple formal syntax that makes it easy to create and manipulate documents by both programs and people, emphasizing its use on the Internet. XML is called extensible because it does not fix the markup used in documents: you can create markup according to the needs of a specific area, limited only by the syntax rules of the XML language. Physically, an XML document comprises entities, each of which can refer to a different entity. Logically, an XML document consists of comments, declarations, elements, and processing instructions. All of this in the document is structured using XML markup.

XML Example
<?xml version = "1.0" encoding = "UTF-8"?>
<Order>
  <Id>14</Id>
  <Customer>Jason</Customer>
  <Price>29.00</Price>
</Order>

What is POST request?

POST is one of the most commonly used HTTP methods. The POST request method is used to files and images to the server, submit web forms, or send any data to the server, including XML and JSON. The post data is included in the body of the POST message. The POST request header fields indicate the data type in the POST message, for example, Content-Type: application/xml for XML. The type and size of POST data is not limited.

POST request example with XML data
POST /echo/post/xml HTTP/1.1
Content-Type: application/xml
Content-Length: 100
Host: reqbin.com

[xml data]

POST XML Data Example

Example of sending XML data to ReqBin echo URL.

POST XML Request Example
POST /echo/post/xml HTTP/1.1
Host: reqbin.com
Content-Type: application/xml
Accept: application/xml
Content-Length: 118
      
<?xml version="1.0" encoding="utf-8"?>
<Request>
    <Login>login</Login>
    <Password>password</Password>
</Request>

The server response to our POST XML request.

Server XML Response Example
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 147
      
<?xml version="1.0" encoding="utf-8"?>
<Response>
    <ResponseCode>0</ResponseCode>
    <ResponseMessage>Success</ResponseMessage>
</Response>

How to get XML from the server?

To get an XML from the server, you need to send an HTTP GET request and include the Accept: application/xml header in your request. The Accept header tells the server that your client is expecting XML. Without this header, the server may return data in a different format. For example, if the server can process both XML and JSON on the same API endpoint, setting the Accept request header to application/xml will tell the server that the client is expecting XML and the server will provide the data in XML rather than JSON format.

Get XML Request Example
GET /echo/get/xml HTTP/1.1
Host: reqbin.com
Accept: application/xml

See also

Generate code snippets for C#/.NET and other programming languages

Convert your POST XML request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the C#/.NET code generator.