Curving curl request in VB.NET

As they are, I'm trying to use the Watson service, which makes an email request to the URL, and the cURL code looks like this. How can I do the equivalent of this query in Visual Studio with the Visual Basic language?

curl -X POST -u "{username}":"{password}" —-header "Content-Type:application/json" --data "{\"input\": {\"text\": \"Turn on the lights\"}, \"context\": {\"conversation_id\": \"1b7b67c0-90ed-45dc-8508-9488bc483d5b\", \"system\": {\"dialog_stack\": [\"root\"], \"dialog_turn_counter\": 1, \"dialog_request_counter\": 1}}}" "https://gateway.watsonplatform.net/conversation/api/v1/workspaces/25dfa8a0-0263-471b-8980-317e68c30488/message?version=2016-09-20"

For more information, documentation url with cURL solution:

https://www.ibm.com/watson/developercloud/conversation/api/v1/

Credentials and everything else is in order. I took it from my Node.js example:

var watson = require('watson-developer-cloud');

var conversation = watson.conversation({
  username: '1793094a-e543-4e3a-891d-4b619f21271d',
  password: 'xjmacpjHceRj',
  version: 'v1',
  version_date: '2016-09-20'
});

// Replace with the context obtained from the initial request
var context = {};

conversation.message({
  workspace_id: '7c7b099b-aed4-4d27-a379-8b2f33644600',
  input: {'text': 'Turn on the lights'},
  context: context
},  function(err, response) {
  if (err)
    console.log('error:', err);
  else
    console.log(JSON.stringify(response, null, 2));
});

VB.NET implementation.

    Dim myReq As HttpWebRequest
    Dim myResp As HttpWebResponse
    Dim reader As StreamReader

    Try
       myReq = HttpWebRequest.Create("https://gateway.watsonplatform.net/conversation/api/v1/workspaces/7c7b099b-aed4-4d27-a379-8b2f33644600/message?version=2016-09-20")

        myReq.Method = "POST"
        myReq.ContentType = "application/json"
        myReq.Headers.Add("Authorization", Convert.ToBase64String(Encoding.Default.GetBytes("1793094a-e543-4e3a-891d-4b619f21271d:xjmacpjHceRj")))
        Dim myData As String = "{input: {text: " + txtEnviar.Text + "}, context: {conversation_id: 1b7b67c0-90ed-45Dc-8508-9488bc483d5b\, system\: {dialog_stack: [root], dialog_turn_counter: 1, dialog_request_counter: 1}}}"

        myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
        myResp = myReq.GetResponse
        Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
        Dim myText As String
        myText = myreader.ReadToEnd()

        txtMuestra.Items.Add(myText)

        Catch ex As Exception
            txtMuestra.Items.Add(ex)
    End Try
End Sub

But I get an authentication error. I assume that the method for sending authentication Not allowed by headers is not entirely correct. I am not a VB.NET user, so I have my own complications.

image error

+4
1
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse

myReq = HttpWebRequest.Create("https://meineURI.net")

myReq.Method = "POST"
myReq.ContentType = "application/json"
myReq.Headers.add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password")))
Dim myData As String = "yourDataHere"
myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
myResp = myReq.GetResponse
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
Dim myText As String
myText = myreader.ReadToEnd
+3

Source: https://habr.com/ru/post/1665943/


All Articles