-3

I have a python code. I need vb.net equivalent of it.

Thanks in advance.

payload2 = {"auth_token": "YOUR_AUTH_TOKEN", "status":"green", "data-title1":"index", "data-title2":"machine1", "data-title3":"package10", "data-title4":"current", "data-title5":"tmp"}
r=requests.post("http://localhost:3030/widgets/hot21",data=json.dumps(payload2))

Extra information Code is invoked in "UiPath community edition"

Last code (thanks to JussiV) is

Imports System.Text
Dim payload2 As New Dictionary(Of String, String)
payload2.Add("auth_token", "YOUR_AUTH_TOKEN")
payload2.Add("status", "green")
payload2.Add("data-title1", "index")
payload2.Add("data-title2", "machine1")
payload2.Add("data-title3", "package10")
payload2.Add("data-title4", "current")
payload2.Add("data-title5", "tmp")
Dim params As String = JsonConvert.SerializeObject(payload2, Formatting.None)
Dim Uri As New Uri(String.Format("http://10.10.115.99:3030/widgets/hot21"))
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
webClient.Headers("content-type") = "application/json"
Dim senddata As Object = JsonConvert.SerializeObject(New With {Key .param = params}).ToString()
reqString = Encoding.Default.GetBytes(senddata)
resByte = webClient.UploadData(Uri, "post", reqString)
resString = Encoding.Default.GetString(resByte)

Last error-messages are

webservice has thrown an exception
Source: Invoke code
Message: Error compiling code
error BC30035: syntax error. At line 1
error BC30561: 'Formatting' is ambiguous, imported from the namespaces or types 'Newtonsoft.Json, System.Xml'. At line 10
error BC30002: Type 'WebClient' is not defined. At line 12
error BC30451: 'Encoding' is not declared. It may be inaccessible due to its protection level. At line 18
error BC30451: 'Encoding' is not declared. It may be inaccessible due to its protection level. At line 20
Exception Type: ArgumentException
JussiV
  • 178
  • 3
  • 15
hasanoa
  • 41
  • 10
  • Did you even try doing it? Show us what you've tried. – JussiV Apr 06 '18 at 08:28
  • Dim payload As String = "{""auth_token"": ""YOUR_AUTH_TOKEN"", ""status"":""green"", ""data-title1"":""indeks"", ""data-title2"":""env"", ""data-title3"":""pakno"", ""data-title4"":""cur"", ""data-title5"":""tmp""}" Dim r As String = "requests.post(""http://localhost:3030/widgets/hot"",data=json.dumps(payload))" – hasanoa Apr 06 '18 at 08:35

1 Answers1

1

Creating a dictionary in VB.net https://www.dotnetperls.com/dictionary-vbnet:

Dim payload2 As New Dictionary(Of String, String>
payload2.add("auth_token", "<token>")
....

Then post the dictionary as JSON:

Dim params As String = JsonConvert.SerializeObject(payload2, Formatting.None)
Dim Uri As New Uri(String.Format("http://localhost:3030/widgets/hot"+indeks))
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
webClient.Headers("content-type") = "application/json"
Dim senddata As Object = JsonConvert.SerializeObject(New With {Key .param = params}).ToString()
reqString = Encoding.Default.GetBytes(senddata)
resByte = webClient.UploadData(Uri, "post", reqString)
resString = Encoding.Default.GetString(resByte)

Edit:

You are missing at least the import for System.Net that has the WebClient class. I'm not sure where/how you import the Newtonsoft.Json as I don't see it in your imports but based on the errors it is imported somewhere.

For the ambiguous import, see this answer for resolving ambiguous imports.

JussiV
  • 178
  • 3
  • 15
  • an exception `Message: Error compiling code
    error BC30561: 'Newtonsoft.Json, System.Xml' 'DataSet' is ambiguous, imported from the namespaces or types 'Formatting'. At line 5'
    error BC30002: Type 'WebClient' is not defined. At line 7
    error BC30451: 'parameters' is not declared. It may be inaccessible due to its protection level. At line 12
    error BC30451: 'Encoding' is not declared. It may be inaccessible due to its protection level. At line 13
    error BC30451: 'Encoding' is not declared. It may be inaccessible due to its protection level. At line 15`
    – hasanoa Apr 06 '18 at 09:31
  • My bad, there was a mistake on the `params` argument name on the `Dim senddata ...` line, I had `parameters` instead of `params`, fixed it on the answer – JussiV Apr 06 '18 at 09:34
  • Thank you JussiV, but other errors are still going on – hasanoa Apr 06 '18 at 09:45
  • What errors would those be? Did you add the required imports? Did you fix the typo I still seem to have there on the `payload2` declaration? You need to show some effort man, ain't nobody gonna do it for you. – JussiV Apr 06 '18 at 10:15
  • I think you cannot read exception that I wrote before. Of course I added "Imports System.Text" line (for Encoding) at the beginning and changed payload2 declaration as payload2.add("auth_token", "YOUR_AUTH_TOKEN") – hasanoa Apr 06 '18 at 10:31
  • Could you edit your question to show what you have right now and where it fails? The code I wrote works on [tio.run](https://tio.run) until the actual posting part as the hostname doesn't resolve there so it's not clear where your issue lies. – JussiV Apr 06 '18 at 10:58
  • 1
    I took a line from your code and the issue was resolved. Thank you. – hasanoa Apr 06 '18 at 14:42