I have created restful wcf as :
<OperationContract()> _
<WebInvoke(Method:="POST", UriTemplate:="/getlatestbill", BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)> _
Function getlatestbill(ByVal usrinfo As String) As String
I have seen many links of POST Restful WCF with ContentXml or a class, but i want to pass JsonString. In above code am passing userinfo as json String which contains id and city. as
Sub Post()
Dim req As HttpWebRequest = Nothing
Dim res As HttpWebResponse = Nothing
Try
Dim strg As String
strg = New JavaScriptSerializer().Serialize(New With {Key .ID = 1, Key .city = "dehradun"})
Const url As String = "http://localhost:1843/mobsrv.svc/getlatestbill"
req = CType(WebRequest.Create(url), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/json; charset=utf-8"
req.Timeout = 30000
'req.Headers.Add("SOAPAction", url)
req.ContentLength = strg.Length
Dim sw = New StreamWriter(req.GetRequestStream())
sw.Write(strg)
sw.Close()
res = CType(req.GetResponse(), HttpWebResponse)
Dim responseStream As Stream = res.GetResponseStream()
Dim streamReader = New StreamReader(responseStream)
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
while passing the json string to the service its throwing error in res.GetResponseStream() as :
The remote server returned an error: (400) Bad Request.
I have seen many SO posts but none of them accept jsonString in post function.
How to pass json String via POST to wcf restful service?