Converting Curl command to Httpwebrequest with request body.

Tahir.Waheed

New member
Joined
Oct 6, 2021
Messages
2
Programming Experience
Beginner
Hello Everyone,

I hope I found the right platform to ask this question. I have searched a lot but didn't find any right answer to my problem.
I have a few API calls that I can run with Curl and they work totally fine. I can easily make GET request and converted that into httpwebrequest and it returns the required response.
However with the POST request I am totally stuck.
Following is the curl command.

VB.NET:
"curl --location --request POST "[URL]http://IP[/URL] : port/copy" --header "Content-Type: application/json" --data-raw "{"file":"//path/Testfile.xlsx"}" >> D:\ Testfile.xlsx"

It makes a post request where it tells the file path to the API which is mentioned in --data-raw and downloads that excel file into the defined path for example in the above case in D:\.
It works just fine when I am running the curl command via CMD. However I need to convert this command into httpwebrequest into VB.NET.

I have tried the following so far after going through many topics online.

VB.NET:
Imports System.IO
Imports System.Net
Imports System.Text
Module
Module1
Sub Main()
       Dim returnValue As String = String.Empty
       Dim request As HttpWebRequest
       Dim response As String
      request = HttpWebRequest.Create("[URL]http://IP[/URL]: port/copy")
      request.Method = "POST"
      request.ContentType = "application/json"
      request.SendChunked = True
      Dim postData As String = "{""{""file"":""//path/Testfile.xlsx""}"" >> D:\Testfile.xlsx}"
     Dim data As Byte() = Encoding.UTF8.GetBytes(postData)
    request.ContentLength = data.Length
    Using requestStream = request.GetRequestStream
requestStream.Write(data, 0, data.Length)
requestStream.Close()
Using responseStream = request.GetResponse.GetResponseStream
 Using reader As New StreamReader(responseStream)
 response = reader.ReadToEnd()
End Using
End Using
End Using
End Sub
End Module"


when I am running this code I get this error: " Additional information: The remote server returned an error: (422) Unprocessable Entity."

I have searched a lot but couldn't find what is wrong with my code. Can someone please help me out here. I would really appreciate that.
Thanks a lot and best regards.
 
Last edited by a moderator:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
An example with HttpClient
VB.NET:
Imports System.Net.Http
VB.NET:
Private client As New HttpClient

Private Async Sub JsonPostSample(url As String, json As String, file As String)
    Using content As New StringContent(json, System.Text.Encoding.UTF8, "application/json")
        Using response = Await client.PostAsync(url, content)
            If response.IsSuccessStatusCode Then
                Using s = Await response.Content.ReadAsStreamAsync, output = IO.File.Create(file)
                    Await s.CopyToAsync(output)
                End Using
            Else
                Debug.WriteLine(response.ReasonPhrase)
            End If
        End Using
    End Using
End Sub
VB.NET:
JsonPostSample("http://ip:port", "{""file"": ""//path/Testfile.xlsx""}", "D:\Testfile.xlsx")
 

Tahir.Waheed

New member
Joined
Oct 6, 2021
Messages
2
Programming Experience
Beginner
An example with HttpClient
VB.NET:
Imports System.Net.Http
VB.NET:
Private client As New HttpClient

Private Async Sub JsonPostSample(url As String, json As String, file As String)
    Using content As New StringContent(json, System.Text.Encoding.UTF8, "application/json")
        Using response = Await client.PostAsync(url, content)
            If response.IsSuccessStatusCode Then
                Using s = Await response.Content.ReadAsStreamAsync, output = IO.File.Create(file)
                    Await s.CopyToAsync(output)
                End Using
            Else
                Debug.WriteLine(response.ReasonPhrase)
            End If
        End Using
    End Using
End Sub
VB.NET:
JsonPostSample("http://ip:port", "{""file"": ""//path/Testfile.xlsx""}", "D:\Testfile.xlsx")
Hi,

Pardon my ignorance but I am a totally beginner with this stuff.
First question, How do I pass this information to the solution above (JsonPostSample("http://ip: port", "{""file"": ""//path/Testfile.xlsx""}", "D:\Testfile.xlsx"))

Secondly, I need it as a totally console application without any user interface. Could you maybe help with that a bit?

Thanks a lot.
 
Last edited:
Top Bottom