Converting Curl command to Httpwebrequest in VB.net

Tahir Waheed 1 Reputation point
2021-10-04T16:01:40.59+00:00

Hello Everyone,

I am new to vb.net!

I would like to convert this command to HTTPwebrequest in vb.net. At first I want to test it locally and then run it from within docker.
However I am doing something wrong here and can't even test it locally.

curl --location --request POST "http://ip:port/copy" --header "Content-Type: application/json" --data-raw "{"file":"//path/Testfile.xlsx"}" >> Testfile.xlsx

I have read a few posts on the internet and tried the following.

Imports System.IO
Imports System.Net
Imports System.Text

Module Module1

    Sub Main()

        Dim request As WebRequest
        request = WebRequest.Create("http://ip:port/copy")
        Dim response As WebResponse
        Dim postData As String = "{"file":"//path/Testfile.xlsx"}" >> Testfile.xlsx"
        Dim data As Byte() = Encoding.UTF8.GetBytes(postData)


        request.Method = "POST"
        request.ContentType = "application/json"
        request.ContentLength = data.Length

        Dim stream As Stream = request.GetRequestStream()
        stream.Write(data, 0, data.Length)
        stream.Close()

        response = request.GetResponse()
        Dim sr As New StreamReader(response.GetResponseStream())


        MsgBox(sr.ReadToEnd)

    End Sub

End Module

But I am not able to compile and run it because I am stuck at defining the correct data syntax which needs to be posted.
Am I not using the correct code ? please help me out in converting the mentioned curl command to vb.net.
Thanks in Advance

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,602 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,658 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. faby m 1 Reputation point
    2022-08-11T22:42:50.16+00:00

    too late but:

    your string must be:

    Dim postData As String = "{""file"":""//path/Testfile.xlsx""}"

    and depending on what http://ip:port/copy expects you should put the new name/location in the json

    Dim postData As String = "{""file"":""path/Testfile.xlsx"",""destinationfile"":""newpath/newTestfile.xlsx""}"

    0 comments No comments