Search code examples
asp.net-mvcpostdotnet-httpclient

HttpClient pass multiple simple parameters


I'm trying to do a POST from one controller to another controller. Both controller's are from different projects. One project is serving to simulate the presentation layer (which I will call the test project here).

From the test project I'm trying to pass 2 simple string parameters to the other controller which I will call the process.

var values = new List<KeyValuePair<string, string>>();
values.Add(new KeyValuePair<string, string>("id", param.Id.Value));
values.Add(new KeyValuePair<string, string>("type", param.Type.Value));
var content = new FormUrlEncodedContent(values);
using (var client = new HttpClient())
{
       client.BaseAddress = new Uri(url);
       client.DefaultRequestHeaders.Clear();
       client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("nl-NL"));

       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

       string token = param.token.Value;
       client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

       var response = client.PostAsync("/api/Process/Product", content).Result;

       if (response.IsSuccessStatusCode)
       {
           var result = response.Content.ReadAsStringAsync().Result;
           return Request.CreateResponse(HttpStatusCode.OK, result);
       }

       return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "fail");
}

And in the process controller, I'm trying to receive it like this:

[HttpPost]
public HttpResponseMessage Product(string id, string type)
{
    return null;
}

But it never reaches this controller. I always get a "not found status code".

So how can I pass 2 simple parameters with HttpClient()?


Solution

  • Use Get instead of Post for simple type parameters.

    using (var client = new HttpClient())
    {
        BaseAddress = new Uri(url);
        client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Clear();
        client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("nl-NL"));
    
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        string token = param.token.Value;
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    
        // New code:
        var response = await client.GetAsync( string.format("api/products/id={0}&type={1}",param.Id.Value,param.Id.Type) ).Result;
        if (response.IsSuccessStatusCode)
        {
            var result = response.Content.ReadAsStringAsync().Result;
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "fail");
    }
    

    In the API side you can do like this.

    [HttpGet]
    public HttpResponseMessage Product(string id, string type)
    {
      return null;
    }