{"id":25873,"date":"2019-12-03T14:37:31","date_gmt":"2019-12-03T13:37:31","guid":{"rendered":"http:\/\/help.qualiware.it\/qw-help\/?p=25873"},"modified":"2020-06-25T11:22:55","modified_gmt":"2020-06-25T09:22:55","slug":"webservice-lettura-di-dati-da-un-servizio-webapi-rest-con-request-body","status":"publish","type":"post","link":"https:\/\/help.qualiware.it\/qw-help\/webservice-lettura-di-dati-da-un-servizio-webapi-rest-con-request-body\/","title":{"rendered":"WebService: lettura di dati da un servizio WebApi REST con parametri nella request body"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">  ' In questo esempio viene effettuata la chiamata ad un servizio WebApi REST (webMethod) che restituisce informazioni in formato Json. \r\n' I parametri anzich\u00e8 essere accodati alla URL sono inseriti nella request body assemblati in formato JSON\r\n\r\nDim jsonr as string\r\ndim webRequest__1 As Net.HttpWebRequest\r\ndim resp As Net.HttpWebResponse \r\ndim ok as boolean\r\ndim responseStream as System.io.Stream \r\ndim sr as System.io.StreamReader\r\nDim httpResponse As net.HttpWebResponse\r\ndim errCode as string\r\nDim obj As Newtonsoft.Json.linq.JObject\r\nDim url As String = \"&lt;mettere qui la URL del servizio&gt;\" \r\nwebRequest__1 = DirectCast(Net.WebRequest.Create(url), Net.HttpWebRequest)\r\nwebRequest__1.Method = \"POST\"\r\nwebRequest__1.ContentType = \"application\/json\"\r\nwebRequest__1.Accept = \"application\/json\"\r\n\t\t\t\t\r\njsonr = \"{\"\r\njsonr += \"\"\"contact\"\": {\"\r\njsonr += \"\"\"email\"\": \"\"\"+q.rowset.fields(\"EMAIL\").value+\"\"\",\"\r\njsonr += \"\"\"lastName\"\": \"\"\"+q.rowset.fields(\"DESCPER\").value+\"\"\",\"\r\njsonr += \"\"\"firstName\"\": \"\"\"\",\"\r\njsonr += \"\"\"phone\"\": \"\"\"+q.rowset.fields(\"TELEFONO\").value+\"\"\"\"\r\njsonr += \"}\"\r\njsonr += \"}\"\r\n\t\t\t\t\t\t\r\nwebRequest__1.ContentLength = jsonr.ToString().Length\r\n\r\nUsing requestWriter2 As New IO.StreamWriter(webRequest__1.GetRequestStream())\r\n  requestWriter2.Write(jsonr)\r\nEnd Using\r\n\t\t\t\t\t\t\r\nresp = DirectCast(webRequest__1.GetResponse(), Net.HttpWebResponse)\r\n\r\nif resp.StatusCode = 200 or resp.StatusCode = 201 then\r\n   responseStream = resp.GetResponseStream()\r\n   sr= new System.IO.StreamReader(responseStream)\r\n   jsonr = sr.ReadToEnd() ' This reads the JSon with the result\r\n\t\t\t\t\t\t\t\r\n   obj=Newtonsoft.Json.linq.JObject.Parse(jsonr)\t\r\n\r\n   ' Questo \u00e8 un esempio di come recuperare i dati dal JSON di risposta\r\n   id=ctype(obj(\"contact\")(\"id\"),Newtonsoft.Json.Linq.JValue).Value  \t\t\t\t\t\t\t\r\nEnd If\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8216; In questo esempio viene effettuata la chiamata ad un servizio WebApi REST (webMethod) che restituisce informazioni in formato Json. &#8216; I parametri anzich\u00e8 essere accodati alla URL sono inseriti nella request body assemblati in formato JSON Dim jsonr as string dim webRequest__1 As Net.HttpWebRequest dim resp As Net.HttpWebResponse dim ok as boolean dim responseStream&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[65],"tags":[71],"acf":[],"_links":{"self":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25873"}],"collection":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/comments?post=25873"}],"version-history":[{"count":0,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25873\/revisions"}],"wp:attachment":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/media?parent=25873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/categories?post=25873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/tags?post=25873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}