Click here to Skip to main content
15,889,730 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I created a WEB API using asp.net with basic authentication.
Here is my get Method link :
localhost:XXX/api/Products/X1

But the client need the request and response in XML format with Products id and authentications credential included in it .

Example :
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ssss/">
  <wsVersion>1</wsVersion>
  <id>user1</id>
  <password>pass1</password>
  <productID>X1</productID>
  <ptype>test</ptype>
</Request>


this is the product controller method for GET

public HttpResponseMessage Get(string id)
     {
         var product = _productService.Get(id);
         if (product != null)
             return Request.CreateResponse(HttpStatusCode.OK, product);
         return Request.CreateErrorResponse(HttpStatusCode.NotFound, "data not found for provided id.");
     }



Can I change the request format as XML inside my WEB API ? or I need to create a WCF service for this ?

What I have tried:

Do I need change the WEBapi to WCF service ?
Posted
Updated 10-Oct-17 21:33pm
Comments
Atlapure Ambrish 11-Oct-17 4:22am    
What was the problem with solution I had provided?
ali_1 11-Oct-17 6:11am    
I am using "Advanced Rest Client" chrome extension . Also added the headers mentioned in XML request format
URL was localhost:XXX/api/Products/ (without Id because ID already added on the header , why its not accepting this request? I am returning the whole data now. Do I need to make any changes on the url ?
Atlapure Ambrish 11-Oct-17 7:16am    
Where you have mentioned this in your original query? before down voting you should share such details. isn't it??
ali_1 11-Oct-17 7:47am    
Product controller code :

I have enabled the basic authentication in it

[ApiAuthenticationFilter]
public class ProductsController : ApiController
{
private readonly IProductService _productService;
public ProductsController()
{
_productService = new ProductService();
}
#region Methods
// GET api/Products/id
public HttpResponseMessage Get(string id)
{
var product = _productService.Get(id);
if (product != null)
return Request.CreateResponse(HttpStatusCode.OK, product);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "data not found for provided id.");
}
// GET api/Products
public HttpResponseMessage GetAll()
{
var products = _productService.GetAll();
if (products.Any())
return Request.CreateResponse(HttpStatusCode.OK, products);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No data found.");
}
***********************************
Product Service code :


public string Get(string i)
{
return _sUnitOfwork.ProductDetails.Get(i);
}
public string GetAll()
{
return _sUnitOfwork.ProductDetails.GetAll();
}
***********************************************************
Product Repository Code :

public string Get(string i)
{
/// code here returns the product Name


return ProductName ;
}



/// <summary>
/// Get all records
///
/// <returns>
public string GetAll()
{
//code here

returns all products Lists
}
********************************

http://localhost:**/api/Products/iD , I used accept: application/xml , still its not accepting the XML and its prompting the window for adding the username and password , if I am giving that its returns the values , but not taking the username and password from XML request from fidler or Advanced rest client

1 solution

You should simply return your object, and shouldn't be concerned about whether its XML or JSON. It is the client's responsibility to request JSON or XML from the web api.

To test this I would simply use Fiddlder and make a request to the web api by specifying request header (either json or xml).

Accept: application/xml
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900