22

I am doing an Asp.Net MVC 4 project and am looking to an internal request (like a proxy) to our api service.

This is what the index method looks like in my controller. I'm stuck at the PostAsync part.

[HttpPost]
public async Task<ActionResult> Index(FormCollection body){

   HttpClient httpClient  = new HttpClient();
   httpClient.BaseAddress = new Uri("http://myapi.com");

   // posts to http://myapi.com/users
   var response = await httpClient.PostAsync("users", body);

   if(response.isSuccessStatusCode) return Json(new {
        status = true,
        url    = response.Content.Url
   });
}

I want to pass my "application/x-form-urlencoded" "body" content to the PostAsync POST method. However, I get an error reading "body is not of type HttpContent".

I can't cast or convert. What now?

Let me know what I'm doing incorrectly here.

Erik

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
Erik5388
  • 2,171
  • 2
  • 20
  • 29
  • `FormCollection` and `HttpContent` are completely different types. Why would you expect that your code would work (with or without a cast)? What you need to do is to create a new `HttpContent`, based on the data in your `FormCollection`. – svick Oct 23 '12 at 18:55
  • Right, Im trying to illustrate what I need done above. I'm looking for more of a walk-through than a simple answer. @svick, do you have an examples of creating a new HttpContent based on a FormCollection set of data? – Erik5388 Oct 23 '12 at 18:58

1 Answers1

38

I'm not entirely sure what you're trying to do, but possibly converting the FormCollection to a dictionary and using the FormUrlEncodedContent class is what you're looking for.

e.g.:

var response = await httpClient.PostAsync("users",
                                          new FormUrlEncodedContent(
                                              body.
                                                  AllKeys.ToDictionary(
                                                      k => k, v => body[v])));
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • 1
    All this time, I was trying to do this using MultipartFormDataContent and failing. Simply using FormUrlEncodedContent with my request works like a charm. Thanks! – killthrush Mar 12 '13 at 17:41
  • is it not better to convert the data to HttpContent? or is HttpContent just another dictionary? – Erik5388 Mar 14 '13 at 13:57
  • 1
    @Erik5388 I think HttpContent is an *abstract base class*. You cannot instantiate that. Instead, u need a concrete class .. which inherits that .. and `FormUrlEncodedContent` i think is one of those :) – Pure.Krome Mar 06 '14 at 11:59
  • 1
    Yes, `HttpContent` is abstract, you need to use one of the concrete classes like `ByteArrayContent`, `FormUrlEncodedContent`, etc... – Peter Ritchie Apr 14 '14 at 22:45
  • @Erik5388 FormUrlEncodedContent is an HttpContent, see MSDN: https://msdn.microsoft.com/en-us/library/system.net.http.formurlencodedcontent.aspx public class FormUrlEncodedContent : ByteArrayContent and public class ByteArrayContent : HttpContent – user3285954 Jul 09 '15 at 22:15