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.
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.
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.
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: