nick libee
asked on
XML data to a Restful URL
Hello all,
Kindly advice how I can send XML data to a Restful URL..
Please provide me some sample codes.
I have created a WCF service but unfortunately its not sending anything and moreover i am getting following error message
The remote server returned an error: (415) Unsupported Media Type.
Following is the WCF code.
Dim xml As String
xml = "http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
Dim url As String = "http://send/p1"
Dim req As HttpWebRequest = DirectCast(WebRequest.Crea te(url), HttpWebRequest)
Dim requestBytes As Byte() = System.Text.Encoding.ASCII .GetBytes( xml)
req.Method = "POST"
req.ContentType = "text/xml;charset=utf-8"
req.ContentLength = requestBytes.Length
Dim requestStream As Stream = req.GetRequestStream()
requestStream.Write(reques tBytes, 0, requestBytes.Length)
requestStream.Close()
Dim res As HttpWebResponse = DirectCast(req.GetResponse (), HttpWebResponse)
If res.StatusCode = HttpStatusCode.OK Then
End If
Dim sr As New StreamReader(res.GetRespon seStream() , System.Text.Encoding.[Defa ult])
Dim backstr As String = sr.ReadToEnd()
sr.Close()
res.Close()
Web.config
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"/>
</defaultProxy>
</system.net>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFa ults="fals e"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnable d="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAll Requests=" true"/>
</system.webServer>
Kindly advice how I can send XML data to a Restful URL..
Please provide me some sample codes.
I have created a WCF service but unfortunately its not sending anything and moreover i am getting following error message
The remote server returned an error: (415) Unsupported Media Type.
Following is the WCF code.
Dim xml As String
xml = "http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
Dim url As String = "http://send/p1"
Dim req As HttpWebRequest = DirectCast(WebRequest.Crea
Dim requestBytes As Byte() = System.Text.Encoding.ASCII
req.Method = "POST"
req.ContentType = "text/xml;charset=utf-8"
req.ContentLength = requestBytes.Length
Dim requestStream As Stream = req.GetRequestStream()
requestStream.Write(reques
requestStream.Close()
Dim res As HttpWebResponse = DirectCast(req.GetResponse
If res.StatusCode = HttpStatusCode.OK Then
End If
Dim sr As New StreamReader(res.GetRespon
Dim backstr As String = sr.ReadToEnd()
sr.Close()
res.Close()
Web.config
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"/>
</defaultProxy>
</system.net>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFa
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAll
</system.webServer>
ASKER
What i want is a WCF Rest service to post XML data .
I just want to post the following XML data. (empno is the input parameter which user will provide in an application.)
Based on the empno i will form a XML and pass it to a restful URL .
Dim xml As String
xml = "http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
I just want to post the following XML data. (empno is the input parameter which user will provide in an application.)
Based on the empno i will form a XML and pass it to a restful URL .
Dim xml As String
xml = "http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
ASKER
Hello any one there??
I have to send data to an external entity using a link which has XML embedded.
Following is the link provided to us by an external entity
xml = "http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
how can i send data ?
I have to send data to an external entity using a link which has XML embedded.
Following is the link provided to us by an external entity
xml = "http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
how can i send data ?
ASKER
I think i am confused and on top of that i am confusing everyone. Sorry for that.
What I want to do is to create a wrapper service for a service provided by an external entity.
For testing purpose, what i normally do is just paste the xml in Postman and using the POST method i send it and the service/xml/whatever... returns me a response.
How can i achieve this? I just want to create a wrapper service.
What I want to do is to create a wrapper service for a service provided by an external entity.
For testing purpose, what i normally do is just paste the xml in Postman and using the POST method i send it and the service/xml/whatever... returns me a response.
How can i achieve this? I just want to create a wrapper service.
"http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
ASKER
hello please help me.
give me some idea ...
give me some idea ...
For testing purpose, what i normally do is just paste the xml in Postman and using the POST method i send it and the service/xml/whatever... returns me a response.In other words: You just want to consume a REST service?
ASKER
Yes thats correct.
Consume it in a WCF service.
actually iam confused.
i have to create a wrapper service or consume the external service .
Consume it in a WCF service.
actually iam confused.
i have to create a wrapper service or consume the external service .
actually iam confused.You're tackling to many things at once. Step by step is the solution.
Use Fiddler on your development machine to look at what Postman is doing. So that you understand, how a REST requerst works.
Cause I already told you, that the payload of a REST request is normally placed into the request body, not the URL.
See Using the REST Services with .NET. You need to copy your XML payload into the request body:
Private Sub GetPOSTResponse(uri As Uri, data As String, callback As Action(Of Response))
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
request.Method = "POST"
request.ContentType = "text/plain;charset=utf-8"
Dim encoding As New System.Text.UTF8Encoding()
Dim bytes As Byte() = encoding.GetBytes(data)
request.ContentLength = bytes.Length
Using requestStream As Stream = request.GetRequestStream()
' Send the data.
requestStream.Write(bytes, 0, bytes.Length)
End Using
request.BeginGetResponse(
Function(x)
Using response As HttpWebResponse = DirectCast(request.EndGetResponse(x), HttpWebResponse)
If callback IsNot Nothing Then
Dim ser As New DataContractJsonSerializer(GetType(Response))
callback(TryCast(ser.ReadObject(response.GetResponseStream()), Response))
End If
End Using
Return 0
End Function, Nothing)
End Sub
ASKER
thanks
yes u r correct
now i am getting the following error message
<p class="heading1">Service</ p>
<p>Endpoint not found.</p>
yes u r correct
now i am getting the following error message
<p class="heading1">Service</
<p>Endpoint not found.</p>
You missed the point about Fiddler: Install it and compare what Postman sends with the data sent by your application.
ASKER
this is from the fiddler
You know what "compare" means?
ASKER
sorry,
Content-Type: application/json
cache-control: no-cache
Postman-Token: 1a450519-fe75-41cd-a156-0e b3608d31c0
User-Agent: PostmanRuntime/7.1.1
Accept: */*
Host: emp-srvr
accept-encoding: gzip, deflate
content-length: 44
Connection: keep-alive
{
"Empno":1234
}
Content-Type: application/json
cache-control: no-cache
Postman-Token: 1a450519-fe75-41cd-a156-0e
User-Agent: PostmanRuntime/7.1.1
Accept: */*
Host: emp-srvr
accept-encoding: gzip, deflate
content-length: 44
Connection: keep-alive
{
"Empno":1234
}
ASKER
added the below in interface
<OperationContract()> _
<WebInvoke(UriTemplate:="M ult?EmpNo= {EmpNo}", BodyStyle:=WebMessageBodyS tyle.Wrapp edRequest, UriTemplate:="SendDetails/ {value,val ue}")> _
Function SendDetails(EmpNo As String) As Response
results in
500 System.ServiceModel.Servic eActivatio nException
from fiddler
HTTP/1.1 500 System.ServiceModel.Servic eActivatio nException
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Content-Length: 0
<OperationContract()> _
<WebInvoke(UriTemplate:="M
Function SendDetails(EmpNo As String) As Response
results in
500 System.ServiceModel.Servic
from fiddler
HTTP/1.1 500 System.ServiceModel.Servic
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Content-Length: 0
1. Start Fiddler
2. Run your web service request in postman.
3. Run your web service request in your application.
4. Stop Fiddler.
5. Compare both sessions.
2. Run your web service request in postman.
3. Run your web service request in your application.
4. Stop Fiddler.
5. Compare both sessions.
ASKER
this is the difference what i can see
content-type is 'application/json';this inspector supports 'x-www-form-urlencoded'onl y in webforms
but where in the services with xml data
it generates the correct xml
content-type is 'application/json';this inspector supports 'x-www-form-urlencoded'onl
but where in the services with xml data
it generates the correct xml
Please, focus, and rephrase your answer.. be verbose, write complete sentences, be precise, add context, add the Fiddler session as attachment...
ASKER
it's difficult for me to do this because the xml data to be passed to the external entity is in server
and my application is in my local machine. ii am doing lots of trial and error method to make this work.
that's the reason why i unable to send you clear messages.
the only issue remaining is Endpoint not found. which i don't know exactly why its appearing.
is it because of the web.config?
and my application is in my local machine. ii am doing lots of trial and error method to make this work.
that's the reason why i unable to send you clear messages.
the only issue remaining is Endpoint not found. which i don't know exactly why its appearing.
is it because of the web.config?
You said, you can send a request successfully using Postman.. is this correct or not?
ASKER
yes that's correct and that's from the server using the raw xml link as given below
"http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
i have postman installed in server and i pass the above as a parameter and get the response (using post)
now in my local machine, using your code i have created a rest service (which will inturn call the above xml link ) and i have deployed it in the server.
Dim xml As String
xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
Dim uri As String = "http://send/p1/?xml"
Dim request As HttpWebRequest = DirectCast(HttpWebRequest. Create(uri ), HttpWebRequest) ----------- this is from your code
i have postman installed in my local machine and using it i am consuming the above rest service
(written by myself with the help of you.)
for this service i am getting Endpoint not found.
web.config
<system.serviceModel>
<services>
<service name="test.testService">
<endpoint binding="webHttpBinding" contract="test.ItestServic e" behaviorConfiguration="web "/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFa ults="fals e"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
hope its clear to you. i am sorry that i am unable to give you the excat fiddler messages due to this.
i dont have visual studio installed in the server.
thanks
"http://send/p1/?xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
i have postman installed in server and i pass the above as a parameter and get the response (using post)
now in my local machine, using your code i have created a rest service (which will inturn call the above xml link ) and i have deployed it in the server.
Dim xml As String
xml=<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
Dim uri As String = "http://send/p1/?xml"
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.
i have postman installed in my local machine and using it i am consuming the above rest service
(written by myself with the help of you.)
for this service i am getting Endpoint not found.
web.config
<system.serviceModel>
<services>
<service name="test.testService">
<endpoint binding="webHttpBinding" contract="test.ItestServic
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFa
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
hope its clear to you. i am sorry that i am unable to give you the excat fiddler messages due to this.
i dont have visual studio installed in the server.
thanks
ASKER
any idea how to resolve this weird issue???
ASKER
hello pls help me
ASKER
hello anyone can you please help me
ASKER
i recreated a new rest project and i can even see the url
but the only issue is when i run in it postman it is showing 404 not found message
any idea how to rectify the issue.
<system.serviceModel>
<services>
<service name="test.testService" behaviorConfiguration="ser viceBehavi or">
<endpoint address="testService.svc" binding="webHttpBinding"
contract="test.ItestServic e"
behaviorConfiguration="web "></endpoi nt>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFa ults="fals e"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
interface
Imports System.ServiceModel
Imports System.Xml
' NOTE: You can use the "Rename" command on the context menu to change the interface name "ItestService" in both code and config file together.
<ServiceContract()>
Public Interface ItestService
<System.ServiceModel.Opera tionContra ct(Name:=" SendEmailE mp")>
<WebInvoke(UriTemplate:="/ SendEmailE mp?Empno={ Empno}", Method:="POST", BodyStyle:=WebMessageBodyS tyle.Wrapp edRequest) > _
Function SendEmailEmp(ByVal Empno As Integer) As Response
End Interface
<DataContract()>
Public Class Response
<DataMember()>
Property ResponseCode() As Integer
<DataMember()>
Property ResponseMessage() As String
End Class
Implementation
Public Function SendEmailEmp(Empno As Integer) As Response Implements ItestService.SendNotificat ion
Dim ResponseInfo As New Response
Dim xml As String
xml = "<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
Dim url As String = "http://send/p1?xml"
Dim req As HttpWebRequest = DirectCast(WebRequest.Crea te(url), HttpWebRequest)
Dim requestBytes As Byte() = System.Text.Encoding.ASCII .GetBytes( xml)
req.Method = "POST"
req.ContentType = "application/xml;charset=u tf-8"
req.Timeout = 30000
req.Headers.Add("SOAPActio n", url)
req.ContentLength = requestBytes.Length
Dim requestStream As Stream = req.GetRequestStream()
requestStream.Write(reques tBytes, 0, requestBytes.Length)
requestStream.Close()
'' set up request
Dim res As HttpWebResponse = DirectCast(req.GetResponse (), HttpWebResponse)
If res.StatusCode = HttpStatusCode.OK Then
End If
Dim sr As New StreamReader(res.GetRespon seStream() , System.Text.Encoding.[Defa ult])
Dim backstr As String = sr.ReadToEnd()
sr.Close()
res.Close()
Return ResponseInfo
End Function
but the only issue is when i run in it postman it is showing 404 not found message
any idea how to rectify the issue.
<system.serviceModel>
<services>
<service name="test.testService" behaviorConfiguration="ser
<endpoint address="testService.svc" binding="webHttpBinding"
contract="test.ItestServic
behaviorConfiguration="web
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFa
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
interface
Imports System.ServiceModel
Imports System.Xml
' NOTE: You can use the "Rename" command on the context menu to change the interface name "ItestService" in both code and config file together.
<ServiceContract()>
Public Interface ItestService
<System.ServiceModel.Opera
<WebInvoke(UriTemplate:="/
Function SendEmailEmp(ByVal Empno As Integer) As Response
End Interface
<DataContract()>
Public Class Response
<DataMember()>
Property ResponseCode() As Integer
<DataMember()>
Property ResponseMessage() As String
End Class
Implementation
Public Function SendEmailEmp(Empno As Integer) As Response Implements ItestService.SendNotificat
Dim ResponseInfo As New Response
Dim xml As String
xml = "<?xml version=""1.0"" encoding=""utf-8""?>" +
"<Emp ID=""1234"">" +
"<EmpDtls>" +
"<Empno>" & Empno & "</empno>" +
"</EmpDtls>" +
"</Emp>"
Dim url As String = "http://send/p1?xml"
Dim req As HttpWebRequest = DirectCast(WebRequest.Crea
Dim requestBytes As Byte() = System.Text.Encoding.ASCII
req.Method = "POST"
req.ContentType = "application/xml;charset=u
req.Timeout = 30000
req.Headers.Add("SOAPActio
req.ContentLength = requestBytes.Length
Dim requestStream As Stream = req.GetRequestStream()
requestStream.Write(reques
requestStream.Close()
'' set up request
Dim res As HttpWebResponse = DirectCast(req.GetResponse
If res.StatusCode = HttpStatusCode.OK Then
End If
Dim sr As New StreamReader(res.GetRespon
Dim backstr As String = sr.ReadToEnd()
sr.Close()
res.Close()
Return ResponseInfo
End Function
Your informations are pretty irritating.
Post the Fiddler sessions content (local Postman and your application). Cause, when the consumed service is a REST service, your additional header (SOAPAction) is nonsense.
Post the Fiddler sessions content (local Postman and your application). Cause, when the consumed service is a REST service, your additional header (SOAPAction) is nonsense.
ASKER
POST http://localhost:53302/testService.svc/SendEmailEmp HTTP/1.1
Content-Type: application/json
cache-control: no-cache
Postman-Token: 845a6569-8675-4e53-af4c-fd 0228b80caf
User-Agent: PostmanRuntime/7.1.1
Accept: */*
Host: localhost:53302
accept-encoding: gzip, deflate
content-length: 44
Connection: keep-alive
{
" Empno":1234
}
HTTP/1.1 404 Not Found
Server: ASP.NET Server/10.0.0.0
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Length: 0
Connection: Close
Content-Type: application/json
cache-control: no-cache
Postman-Token: 845a6569-8675-4e53-af4c-fd
User-Agent: PostmanRuntime/7.1.1
Accept: */*
Host: localhost:53302
accept-encoding: gzip, deflate
content-length: 44
Connection: keep-alive
{
" Empno":1234
}
HTTP/1.1 404 Not Found
Server: ASP.NET Server/10.0.0.0
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Length: 0
Connection: Close
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
WCF implies SOAP. SOAP usese a pretty complex XML protocol to exchange data.
REST uses http and its verb semantics. Posting data is done by placing the data in the request body. Not the URL. The URL is only for representing the entities.
So what are you really doing?