Page 1 of 1

how to send byte stream to POST?

Posted: Wed Sep 11, 2013 7:23 am
by Ram

Hi all,

I'm having a service that i need to POST byte array stream to server, how do i pass the byte array into the POST request?

Many thanks in advance!
Best,
R.

==========================================================

here's an example of a working VB.Net code that does it, this part is the one im trying to achieve:

pre
stream = .GetRequestStream()
stream.Write(data, 0, data.Length)
stream.Close()
/pre

here's the full code:
pre
Module GoxAPI

Code: Select all

     Sub Main() 
             ' Example API implementation in VBA 
             ' Not complete 
             ' VBA is hard to implement the API in, would recommend against 
             ' Still need to decode the JSON response 

             Dim goxKey, goxSec, goxSign, basePath, nonce, method, postData, response As String 
             ' MtGox API Specifics 
             Dim version As Integer : version = 2 
             basePath = "https://data.mtgox.com/api/" & version.ToString() & "/" 
             nonce = Math.Round(DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds).ToString() & "000" 

             ' Fill these in 
             goxKey = "Your API Key" 
             goxSec = "Your API Secret" 

             ' The API method 
             method = "BTCUSD/money/ticker" 
             postData = "nonce=" & nonce 

             ' Compute HMAC 
             goxSign = HmacSHA512(method & Chr(0) & postData, goxSec) 

             ' Make and send Request 
             Dim request As System.Net.HttpWebRequest 
             Dim stream As System.IO.Stream 
             Dim wresponse As System.Net.HttpWebResponse 
             Dim asc As Object : asc = New System.Text.UTF8Encoding() 
             Dim data() As Byte : data = asc.GetBytes(postData) 
             Dim path As String : path = basePath & method 
             request = System.Net.WebRequest.Create(path) 

             Console.WriteLine("Path: " & path) 
             Console.WriteLine("Data: " & postData) 
             Console.WriteLine(data.Length & "/" & postData.Length) 

             With request 
                     .Method = "POST" 
                     .ContentLength = data.Length 
                     .UserAgent = "btc_bot" 
                     .Headers.Add("Rest-key", goxKey) 
                     .Headers.Add("Rest-sign", goxSign) 

                     stream = .GetRequestStream() 
                     stream.Write(data, 0, data.Length) 
                     stream.Close() 

                     Try 
                             wresponse = .GetResponse() 
                     Catch ex As System.Net.WebException 
                             wresponse = ex.Response 
                             Console.WriteLine("HTTP Code: " & wresponse.StatusCode) 
                     End Try 

                     stream = wresponse.GetResponseStream() 
                     Dim reader As New System.IO.StreamReader(stream) 
                     response = reader.ReadToEnd() 

                     reader.Close() 
                     stream.Close() 
                     wresponse.Close() 
             End With 

             ' MsgBox(response) 
             Console.WriteLine("Response: " & response) 
             Console.ReadKey() 

             ' Check that the request was successful 
             ' Now, response contains a JSON string 
             ' It needs to be decoded 
             ' You can check the 'result' key for 'success' 
             ' Otherwise it may be 'error' 
     End Sub 

     Public Function HmacSHA512(ByVal msg As String, ByVal secret As String) 
             Dim asc, enc As Object 
             Dim bytes() As Byte 
             Dim bMsg() As Byte 

             asc = New System.Text.UTF8Encoding() 
             enc = New System.Security.Cryptography.HMACSHA512() 

             bMsg = asc.GetBytes(msg) 
             enc.Key = System.Convert.FromBase64String(secret) 

             bytes = enc.ComputeHash(bMsg) 
             HmacSHA512 = System.Convert.ToBase64String(bytes) 

             asc = Nothing 
             enc = Nothing 
     End Function 

End Module
/pre


how to send byte stream to POST?

Posted: Wed Sep 11, 2013 10:47 am
by Maryna Brodina

Hello! Working on it, I'll update.


how to send byte stream to POST?

Posted: Wed Sep 11, 2013 10:56 am
by Ram

Great!!! holding my breath :)


how to send byte stream to POST?

Posted: Wed Sep 11, 2013 1:01 pm
by Maryna Brodina

To send byte array stream in POST request you have to use ArrayBuffer, here is an example Sending and Receiving Binary Data
codevar myArray = new ArrayBuffer(512);
var longInt8View = new Uint8Array(myArray);

for (var i=0; i< longInt8View&#46;length; i++) {
longInt8View = i % 255;
}

var xhr = new XMLHttpRequest;
xhr&#46;open("POST", url, false);
xhr&#46;send(myArray);/code
to convert Unicode string to ArrayBuffer with JS you can use the following solution http://stackoverflow.com/questions/69...
codefunction utf8AbFromStr(str) {
var strUtf8 = unescape(encodeURIComponent(str));
var ab = new Uint8Array(strUtf8&#46;length);
for (var i = 0; i < strUtf8&#46;length; i++) {
ab = strUtf8&#46;charCodeAt(i);
}
return ab;
}/code


how to send byte stream to POST?

Posted: Wed Sep 11, 2013 3:11 pm
by Ram

Thank you so very much for that!!

Is it possible to combine that with an existing rest service call? i have the following service:

Image

Image

i'm trying to send the byte array stream in the above POST request...

i sure hope you'll figure this out as this is a real make or break situation for that project.

once again, many many thanks!!
Best,
R.


how to send byte stream to POST?

Posted: Wed Sep 11, 2013 4:46 pm
by Kateryna Grynko

Hi Ram,

You can add request parameter with data from localStorage variable (when creating mapping). A needed array should be saved in localStorage variable beforehand.


how to send byte stream to POST?

Posted: Wed Sep 11, 2013 9:32 pm
by Ram

Dear Marina & Katya,

Thank you both soooooo much!!!!!

Cheers!
R.