2
votes

I'm trying to test my web api but I can't post image as byte array. How can I do it? I'm using File Upload Parameter and specify content-type application/octet-stream but I'm getting 415 unsupported media type. How can I post image as byte array ?

Here is my request:

enter image description here

And Form Post Parameter Properties:

enter image description here

Here is my web api post method:

enter image description here

Here is my webtest request log:

enter image description here

Edit: @nick_w has great answer but I found another way. I generate code from my webtest file. Correct test request should be shown as below:

WebTestRequest request2 = new WebTestRequest("http://url/api/SendStream");
request2.Method = "POST";
request2.Headers.Add(new WebTestRequestHeader("Content-Type", "application/octet-stream"));
request2.QueryStringParameters.Add("JobId", this.Context["JobId"].ToString(), false, false);

FileStream oFileStream = new FileStream("4_e.jpg", FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[oFileStream.Length];
oFileStream.Read(bytes, 0, System.Convert.ToInt32(oFileStream.Length));
oFileStream.Close();

BinaryHttpBody request2Body = new BinaryHttpBody();
request2Body.ContentType = "application/octet-stream";
request2Body.Data = bytes;
request2.Body = request2Body;
request2.SendChunked = true;
request2.Timeout = 10000000;

yield return request2;
request2 = null;
1
Hi, Is it possible to add Form data along with Web Request? I'm facing problem that on server when Request.Files["SomeName"] is tried, it returns null. - Hiren Desai

1 Answers

1
votes

I expect you are having issues binding the stream parameter to a byte array. Based on information found here and in this question, try changing your method to something like:

public async Task<ResponseEntity<SendStreamResponse>> Post([FromUri]int jobId)
{
    byte[] stream = await Request.Content.ReadAsByteArrayAsync();

    return await SendStreamAsync(jobId, stream);
}