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>


 Samples
JonyTolengoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Navneet.Net Full Stack DeveloperCommented:
H!

for me both are JSON

Check again

Thanks!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Navneet.Net Full Stack DeveloperCommented:
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}
0
JonyTolengoAuthor Commented:
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.



0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Navneet.Net Full Stack DeveloperCommented:
Hi!

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

Thanks!
0
JonyTolengoAuthor Commented:
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

0
JonyTolengoAuthor Commented:
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).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WCF

From novice to tech pro — start learning today.