开发者

How can I get more detail on why this REST service request is not working?

开发者 https://www.devze.com 2023-01-16 03:01 出处:网络
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

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消