Link to home
Start Free TrialLog in
Avatar of JonyTolengo
JonyTolengoFlag for Argentina

asked on

Why a REST service returns a JSON result insde of a XML ?

Hi,
 
I already built all my  "REST" Services according the samples attached here (using the second sample).
 
Now I am starting to try it and don´t works as I expected.
 
Please, pay attention on this.
 
The first sample works fine, it returns a JSON response, but the second sample, returns a JSON inside of a XML.
 
Where is my mistake?
 
You can try the following links, to see the responses:
 
http://50.63.16.67:81/MobileRestServices/option1
(please see the source of the result)   iS OK, IS A JSON RESULT
 
http://50.63.16.67:81/MobileRestServices/option2
(please see the source of the result)   IS BAD, IS A JSON INSIDE A XML
 
Thanks in advance, and have a Hapy New Year...
 
Jonny
 
 
 
 
 
 
 

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Net.Mail
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections

Namespace MobileRestServices

        Public Class SampleOptions
            Public Property id As Integer
            Public Property title As String
        End Class
        Public Class Sample
            Public Property status As String
            Public Property userId As Integer
            Public Property name As String
            Public Property rol As Integer
            Public Property options As List(Of SampleOptions)
        End Class
        <ServiceContract()> _
        <AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
        <ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerCall)> _
        Public Class MobileRestServices
            <WebGet(UriTemplate:="Option1", _
                ResponseFormat:=WebMessageFormat.Json, _
                BodyStyle:=WebMessageBodyStyle.Bare)>
            Public Function ManageMobileUsers1() As Stream
            Dim Sample As String = "{""status"":""ok"",""userId"":23847,""name"":""Luis Oliveira"",""rol"":0,""options"":[{""id"":1,""title"":""Ventas""},{""id"":2,""title"":""Compras""},{""id"":3,""title"":""Recursos Humanos""}]}"
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"
                Return New MemoryStream(Encoding.UTF8.GetBytes(Sample))
            End Function
            <WebGet(UriTemplate:="Option2", _
                ResponseFormat:=WebMessageFormat.Json, _
                BodyStyle:=WebMessageBodyStyle.Bare)>
            Public Function ManageMobileUsers2() As Sample
                Dim sample As New Sample
                sample.name = "Luis Oliveira"
                sample.rol = 0
                sample.status = "ok"
                sample.userId = 23847
                sample.options = New List(Of SampleOptions)
                sample.options.Add(New SampleOptions With {.id = 1, .title = "Ventas"})
                sample.options.Add(New SampleOptions With {.id = 2, .title = "Compras"})
                sample.options.Add(New SampleOptions With {.id = 3, .title = "Recursos Humanos"})
                Return sample
            End Function
        End Class

End Namespace
 

My Web.config:
 
 
 

<?xml version="1.0"?>
<configuration>
 
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <!--<pages validateRequest="false" />-->
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
           <!--<staticContent>
              <mimeMap fileExtension=".json" mimeType="text/plain" />
           </staticContent>-->
  </system.webServer>

  <system.serviceModel>

    <bindings>
      <basicHttpBinding>
        <binding transferMode="Streamed"/>
      </basicHttpBinding>
    </bindings>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!--
            Configure the WCF REST service base address via the global.asax.vb file and the default endpoint
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>


 User generated image
ASKER CERTIFIED SOLUTION
Avatar of Navneet Hegde
Navneet Hegde
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OPTION 1
{"status":"ok","userId":23847,"name":"Luis Oliveira","rol":0,"options":[{"id":1,"title":"Ventas"},{"id":2,"title":"Compras"},{"id":3,"title":"Recursos Humanos"}]}

OPTION 2
{"name":"Luis Oliveira","options":[{"id":1,"title":"Ventas"},{"id":2,"title":"Compras"},{"id":3,"title":"Recursos Humanos"}],"rol":0,"status":"ok","userId":23847}
Avatar of JonyTolengo

ASKER

Thanks for your responses.

But if you see the "source" (source option int google crome for example) in the response of OPTION 2, you will see:

<Sample xmlns="http://schemas.datacontract.org/2004/07/MobileRestServices.MobileRestServices" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><name>Luis Oliveira</name><options><SampleOptions><id>1</id><title>Ventas</title></SampleOptions><SampleOptions><id>2</id><title>Compras</title></SampleOptions><SampleOptions><id>3</id><title>Recursos Humanos</title></SampleOptions></options><rol>0</rol><status>ok</status><userId>23847</userId></Sample>

And this is a XML response, with a JSON string.



Hi!

try
 <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false"/>

Thanks!
Thanks again...

But I already done a few days ago.... (ups) thanks.

see my web.config.

 
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <!--<pages validateRequest="false" />-->
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <!--<staticContent>
      <mimeMap fileExtension=".json" mimeType="text/plain" />
    </staticContent>-->
  </system.webServer>

  <system.serviceModel>

    <!--<bindings>
      <basicHttpBinding>
        <binding transferMode="Streamed"/>
      </basicHttpBinding>
    </bindings>-->

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.vb file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>

Open in new window

The response it's correct. If the developer from the IOS app wants a JSON response, they will not add an Accept: application/xml header in their requests, so the response will not be sent as XML. They'll either add something like Accept: application/json (in which WCF will honor that request and return JSON), or they won't add any Accept header, in which case the response type becomes what you specified in the RequestFormat property of the [WebGet] attribute (i.e., JSON).