-2

My API i built on C#.net

It suppose to return JSON file.

but it wraps it with XML header!!

Is there a way to get rid of the header and only send the JSON text?

Here is an example

I am calling

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<link type="text/css" rel="stylesheet" id="dark-mode-custom-link"/>
<link type="text/css" rel="stylesheet" id="dark-mode-general-link"/>
<style lang="en" type="text/css" id="dark-mode-custom-style"/>
<style lang="en" type="text/css" id="dark-mode-native-style"/>
<style lang="en" type="text/css" id="dark-mode-native-sheet"/>
[
   {
      "PtsID":1,
      "PtsCode":"10014352-1208570.pdf",
      "PtsName":"10014352-1208570.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":1,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":1,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":1,
      "PtsCode":"10014352-1208570.pdf",
      "PtsName":"10014352-1208570.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":1,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":1,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":1,
      "PtsCode":"10014352-1208570.pdf",
      "PtsName":"10014352-1208570.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":1,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":1,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":1,
      "PtsCode":"10014352-1208570.pdf",
      "PtsName":"10014352-1208570.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":1,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":1,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":1,
      "PtsCode":"10014352-1208570.pdf",
      "PtsName":"10014352-1208570.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":1,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":1,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":1,
      "PtsCode":"10014352-1208570.pdf",
      "PtsName":"10014352-1208570.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":1,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":1,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":2,
      "PtsCode":"10099142-1836694.pdf",
      "PtsName":"10099142-1836694.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":2,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":2,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":2,
      "PtsCode":"10099142-1836694.pdf",
      "PtsName":"10099142-1836694.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":2,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":2,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":2,
      "PtsCode":"10099142-1836694.pdf",
      "PtsName":"10099142-1836694.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":2,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":2,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   },
   {
      "PtsID":2,
      "PtsCode":"10099142-1836694.pdf",
      "PtsName":"10099142-1836694.pdf",
      "DOB":null,
      "Gender":null,
      "VisitID":2,
      "VisitType":"Inpatient",
      "VisitDate":"10/01/2022",
      "DischargeDate":"10/01/2022",
      "RepID":2,
      "RepDate":"10/01/2022",
      "RepType":"Unidentified",
      "RepDesc":null,
      "VisitStatus":"Updated",
      "RepStatus":"Completed"
   }
]

</string>

I want the JSON file without the header

I tried with Edge browser there is still a header and also with Postman

Here is my code

namespace CDAPIs.Controllers
{
    public class CallSPController : ApiController
    {

        [Route("api/CallSP")]

    public string Get(string UserID = null, string Tokin = null, string SP = null, string Type = null, string Ps = null, string P0 = null)
    {
        string sDBConn = Functions.GetConn();
        string[] Parms = Ps.Split(',');
        SqlConnection conn = new SqlConnection(sDBConn);
        conn.Open();

        SqlCommand cmd = new SqlCommand(SP, conn);
        cmd.CommandTimeout = 0;
        cmd.CommandType = CommandType.StoredProcedure;

        string P = "";
        cmd.Parameters.Add(new SqlParameter("@P0", P0));
        

        SqlDataReader rdr = cmd.ExecuteReader();
        var dt = new DataTable();
        dt.Load(rdr);
        List<DataRow> result = dt.AsEnumerable().ToList();
        rdr.Close();
        conn.Close();
        conn.Dispose();
        string json = Functions.DataTableToJSON(dt);
        //return Json(result, JsonRequestBehavior.AllowGet);
        return json;
    }

    public static string DataTableToJSON(DataTable dt)
    {
        string JSONString = string.Empty;
        string json = JsonConvert.SerializeObject(dt, Formatting.Indented);

        //JSONString = JSONConvert.SerializeObject(table);
        return json;
    }

Update: This API will be used for Desktop and Web applications.

asmgx
  • 7,328
  • 15
  • 82
  • 143

1 Answers1

1

if you are using mvc5 try to add this lines to WebApiConfig.cs after config.Routes.MapHttpRoute

config.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
config.Formatters.Add(GlobalConfiguration.Configuration.Formatters.JsonFormatter);
Serge
  • 40,935
  • 4
  • 18
  • 45