-5

I'm totally new to CURL and would like to know how to convert the following command to .net using httpWebRequest (that's my guess)

curl -X POST http://127.0.0.1:8042/instances --data-binary @CT.X.1.2.276.0.7230010.dcm

I really haven't tried anything as frankly I have no idea where to start.

ranasrule
  • 33
  • 1
  • 10
  • What parts of this curl command specifically are unclear to you for translating to a HttpWebRequest? – Jonathon Chase Jan 05 '18 at 21:03
  • If you dont know where to start, you have not done enough research. This is not a tutorial site or code conversion service. – Ňɏssa Pøngjǣrdenlarp Jan 05 '18 at 21:35
  • [First item in list](https://www.bing.com/search?q=translate%20curl%20commands%20to%20response%20calls%20c%23&qs=n&form=QBRE&sp=-1&pq=translate%20curl%20commands%20to%20response%20calls%20c%23&sc=0-44&sk=&cvid=A65D24A2F51F4CCCB668804175612970) – vipersassassin Jan 05 '18 at 21:42
  • What is the dataType of data-binary? you can customize the example https://learn.microsoft.com/en-us/dotnet/framework/network-programming/how-to-send-data-using-the-webrequest-class, and define url, and contentType for POST method. – M.Hassan Jan 05 '18 at 21:54
  • @M.Hassan the dataType is DICOM image file – ranasrule Jan 05 '18 at 21:58
  • @JonathonChase mainly this part `--data-binary @CT.X.1.2.276.0.7230010.dcm` – ranasrule Jan 05 '18 at 21:59
  • So, you need to upload that DICOM image to the the requested url. Use this code to upload file: https://stackoverflow.com/a/567460/3142139 – M.Hassan Jan 05 '18 at 22:06
  • @ranasrule You'll want to read the file (`CT.X.1.2.276.0.7230010.dcm`) to a byte array with `File.ReadAllBytes`. Set the request's content type to `multipart/form-data` and write the byte array to the request stream. – Jonathon Chase Jan 05 '18 at 22:08
  • @M.Hassan I also then need to check the response field of "Status" which will be JSON like this `{ "ID" : "e87da270-c52b-4f2a-b8c6-bae25928d0b0", "Path" : "/instances/e87da270-c52b-4f2a-b8c6-bae25928d0b0", "Status" : "Success" }` – ranasrule Jan 05 '18 at 22:13

1 Answers1

0

Here is what I came up with based on the example posted by @M.Hassan

Public Function UPLOAD_FILE(filepath As String) As String
    Dim request As WebRequest = WebRequest.Create("http://localhost:8042/instances ")
    request.Method = "POST"
    Dim byteArray As Byte() = File.ReadAllBytes(filepath)
    request.ContentType = "application/x-www-form-urlencoded"
    request.ContentLength = byteArray.Length
    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()
    Dim response As WebResponse = request.GetResponse()
    Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
    dataStream = response.GetResponseStream()
    Dim reader As StreamReader = New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    reader.Close()
    dataStream.Close()
    response.Close()
    Return responseFromServer
End Function
ranasrule
  • 33
  • 1
  • 10