views:

22

answers:

1

My goal was to create a REST service that will allow me to POST and GET images. I've got the GET portion working, however I'm having difficulty posting an image to the service I created. Using the WCF REST Service Template 40 (CS) template I created the following method to handle my POST.

[WebInvoke(Method = "POST", UriTemplate = "upload/?fileName={fileName}")]
public void AddImageToStore(Stream file, string fileName)
{
// Implementation goes here.  For now method is empty.
}

Below is the code I'm using to consume the above method. If I uncomment line 3 and send nothing in the body everything works. If I uncomment line 4 and send text in the body no error. However, if I send the image in a stream I receive an error "BadRequest (400) is not one of the following: OK (200)..."

Stream theStream = new MemoryStream(File.ReadAllBytes(@"C:\test.jpg"));
Uri uri = new Uri("http://127.0.0.1:50001/MediaService/upload/?fileName=test.jpg");
HttpClient client = new HttpClient(uri);
// HttpContent content = HttpContent.CreateEmpty(); //Status successful
// HttpContent content = HttpContent.Create("test", Encoding.UTF8); //Status successful
// HttpContent content = HttpContent.Create(theStream, "image/jpeg", theStream.Length); //Error when I send a stream!

HttpResponseMessage response = client.Post(uri, content);
response.EnsureStatusIsSuccessful();

This is my first attempt at consuming a REST service. I'm stuck at the moment at how to debug this. How can I get more detail on what the real error is? The "BadRequest (400)..." error message is not very helpful.

A: 

After a lot of trial and error I was able to answer my own question.

It turns out I was sending files larger than the default buffer pool would accept. I ended up tweaking the default values in my web.config for MaxBufferPoolSize, MaxBufferSize, and MaxReceivedMessageSize

Cory