JonyTolengo
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.Activa tion
Imports System.ServiceModel.Web
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Runtime.Serializati on
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()> _
<AspNetCompatibilityRequir ements(Req uirementsM ode:=AspNe tCompatibi lityRequir ementsMode .Allowed)> _
<ServiceBehavior(InstanceC ontextMode :=Instance ContextMod e.PerCall) > _
Public Class MobileRestServices
<WebGet(UriTemplate:="Opti on1", _
ResponseFormat:=WebMessage Format.Jso n, _
BodyStyle:=WebMessageBodyS tyle.Bare) >
Public Function ManageMobileUsers1() As Stream
Dim Sample As String = "{""status"":""ok"",""user Id"":23847 ,""name"": ""Luis Oliveira"",""rol"":0,""opt ions"":[{" "id"":1,"" title"":"" Ventas""}, {""id"":2, ""title"": ""Compras" "},{""id"" :3,""title "":""Recur sos Humanos""}]}"
WebOperationContext.Curren t.Outgoing Response.C ontentType = "application/json; charset=utf-8"
Return New MemoryStream(Encoding.UTF8 .GetBytes( Sample))
End Function
<WebGet(UriTemplate:="Opti on2", _
ResponseFormat:=WebMessage Format.Jso n, _
BodyStyle:=WebMessageBodyS tyle.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 runAllManagedModulesForAll Requests=" true">
<add name="UrlRoutingModule" type="System.Web.Routing.U rlRoutingM odule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d 50a3a" />
</modules>
<!--<staticContent>
<mimeMap fileExtension=".json" mimeType="text/plain" />
</staticContent>-->
</system.webServer>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding transferMode="Streamed"/>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled ="true" multipleSiteBindingsEnable d="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" automaticFormatSelectionEn abled="tru e"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
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.Activa
Imports System.ServiceModel.Web
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Runtime.Serializati
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()> _
<AspNetCompatibilityRequir
<ServiceBehavior(InstanceC
Public Class MobileRestServices
<WebGet(UriTemplate:="Opti
ResponseFormat:=WebMessage
BodyStyle:=WebMessageBodyS
Public Function ManageMobileUsers1() As Stream
Dim Sample As String = "{""status"":""ok"",""user
WebOperationContext.Curren
Return New MemoryStream(Encoding.UTF8
End Function
<WebGet(UriTemplate:="Opti
ResponseFormat:=WebMessage
BodyStyle:=WebMessageBodyS
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 runAllManagedModulesForAll
<add name="UrlRoutingModule" type="System.Web.Routing.U
</modules>
<!--<staticContent>
<mimeMap fileExtension=".json" mimeType="text/plain" />
</staticContent>-->
</system.webServer>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding transferMode="Streamed"/>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment
<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" automaticFormatSelectionEn
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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><S ampleOptio ns><id>1</ id><title> Ventas</ti tle></Samp leOptions> <SampleOpt ions><id>2 </id><titl e>Compras< /title></S ampleOptio ns><Sample Options><i d>3</id><t itle>Recur sos Humanos</title></SampleOpt ions></opt ions><rol> 0</rol><st atus>ok</s tatus><use rId>23847< /userId></ Sample>
And this is a XML response, with a JSON string.
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><S
And this is a XML response, with a JSON string.
Hi!
try
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEn abled="fal se"/>
Thanks!
try
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEn
Thanks!
ASKER
Thanks again...
But I already done a few days ago.... (ups) thanks.
see my web.config.
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>
ASKER
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).
{"status":"ok","userId":23
OPTION 2
{"name":"Luis Oliveira","options":[{"id"