2

I have the following code in an MVC app controller to send some data to be stored in an Archive table using EF6 via a WebAPI2 call.

I'm getting a "Cannot send a content-body with this verb-type" even though I'm setting to POST and the api call is defined to accept only POST.

What in the world am I doing wrong and how can I fix it?

ArchiveUploadModel.ArchiveUpload obj = new ArchiveUploadModel.ArchiveUpload(); obj.LT = LT; obj.PID = PID.ToString(); obj.Title = "Ex Review"; obj.HTML = message.Body; // the HTML is a rendered HTML email message if (!string.IsNullOrEmpty(obj.HTML)) { HttpWebRequest req = HttpWebRequest.Create("http://example.com/MyApp/api/UploadToArchive") as HttpWebRequest; request.ContentType = "application/json"; request.Method = "POST"; string json = JsonConvert.SerializeObject(obj); using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); } } using (HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse) { using (StreamReader reader = new StreamReader(webresponse.GetResponseStream())) { string response = reader.ReadToEnd(); } } 

This is the code for my WebAPI call:

[HttpPost] [Route("api/UploadToArchive")] [EnableCors("http://example.com", // Origin "Accept, Origin, Content-Type, Options", // Request headers "POST", // HTTP methods PreflightMaxAge = 600 // Preflight cache duration )] public IHttpActionResult UploadToArchive(ArchiveUpload upload) { string HTML = upload.HTML; string Title = upload.Title; string LT = upload.LT; string lt = getLT(upload.PID); // essentially secure checking to see if it matches passed LT. if (lt == LT) { // Upload the file to the archive using the ArchiveRepository's UpdateArchive() function: _ArchiveRepository.UpdateArchive(HTML, System.Web.HttpUtility.HtmlDecode(Title), "", upload.PID); return Ok(PID); } else { return BadRequest("Invalid LT"); } } 

ArchiveUpload model definition in both applications:

public class ArchiveUpload { public string LT { get; set; } public string PID { get; set; } public string Title { get; set; } public string HTML { get; set; } } 
2
  • This may be of use. HttpClient is easier to work with than HttpWebRequest. stackoverflow.com/questions/4015324/http-request-with-post My money would be on some form-formatting issue. Commented Sep 8, 2015 at 20:37
  • Post as answer, please. Commented Sep 8, 2015 at 21:26

1 Answer 1

2

Better try to use the Microsoft Http Client Libraries. You can install it from nuget and here you find examples calling Web API using different HTTP verbs

Sign up to request clarification or add additional context in comments.

1 Comment

Did this but having another issue that is not related.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.