Home > OS >  How do I send a HTTP PUT request with HttpClient?
How do I send a HTTP PUT request with HttpClient?

Time:08-21

I have a web service that I'm trying to write to, but every time I try to send a PUT request to it I get a 'Bad Request(400)' Error. I'm using the ASP.NET MVC framework and I have everything set up in one of my controllers. I have a model as well that models the data that should be sent to the API. This is how I have everything set up:

PUT FUNCTION

public string putFunction(ItemOject item)
    {
        // PROVIDED DETAILS
        string API_PATH = "API_PATH";

        // CONTENT
        var content = new StringContent(JsonConvert.SerializeObject(item));
        
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("Key1", KEY_1);
        client.DefaultRequestHeaders.Add("Key2",KEY_2);
        client.DefaultRequestHeaders.Add("Key3", KEY_3);

        // KEYS and BASE_URL are defined outside the function
        var response = client.PutAsync($"{BASE_URL}{API_PATH}", content).Result;

        return response.ToString();
      
    }

This is what the param 'item' is getting passed:

ItemObject item = new ItemObject
        {
            Name = "Test2",
            Errors = null,
            ID = "8c785cd5-673a-4b3c-b97d-b8446dad401a",
            OwnerID = "30e5772a-a11c-4172-96ae-cc850bd6509a"
        };

And this is the response I get:

StatusCode: 400, ReasonPhrase: 'Bad Request'

I'm positive all the KEYS are setup correctly and the URL is correct. The endpoint does work because I've tested it in Postman with no issue. The only thing now is that I can't send the HTTP PUT request from this function. I've played around to make sure that the data I'm sending in Postman is almost exactly what I'm sending from this function. I don't know what I'm missing or what more tests I could be trying.

This is my Postman request:

{"Item": 
    {
        "Errors": null,
        "ID": "8c785cd5-673a-4b3c-b97d-b8446dad401a",
        "Name": "Test1",
        "OwnerID": "00000000-0000-0000-0000-000000000000"
    }
}

And that works fine in Postman if I were to again send a GET request, it would retieve that said data. But I can't do that within ASP.NET without getting the "Bad Request(400)" Error.

What I've tried so far:

Can't find how to use HttpContent

Send ASP.NET MVC HttpContext to Web Api HttpContext

https://www.c-sharpcorner.com/article/asp-net-web-api-using-mvc-entity-framework-and-httpclient-for-get-and-post-with/

CodePudding user response:

Serializing your current object would result in this:

{
    "Errors": null,
    "ID": "8c785cd5-673a-4b3c-b97d-b8446dad401a",
    "Name": "Test1",
    "OwnerID": "00000000-0000-0000-0000-000000000000"
}

But what you are passing into Postman is this:

{
    "Item": 
    {
        "Errors": null,
        "ID": "8c785cd5-673a-4b3c-b97d-b8446dad401a",
        "Name": "Test1",
        "OwnerID": "00000000-0000-0000-0000-000000000000"
    }
}

So your model should look more like:

ItemObject item = new ItemObject()
{
    Item  = new ItemSubObject()
    {
        Name = "Test2",
        Errors = null,
        ID = "8c785cd5-673a-4b3c-b97d-b8446dad401a",
        OwnerID = "30e5772a-a11c-4172-96ae-cc850bd6509a"
    }
};
  • Related