-1

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/

Vincent
  • 81
  • 2
  • 2
  • 10
  • Have you tried mocking your request up in postman to make sure your request to the endpoint is correct? Postman also has a cool feature that generates c# code from a working request – M Leipper Aug 19 '22 at 20:53
  • Based on what I see you could also use a post request here. Is the API endpoint specifically looking for a `PUT` or can you use a post? – mathis1337 Aug 19 '22 at 21:00
  • @MichaelLeipper yep, I've used the working request from Postman in my function in substitution for the `item` param, but instead of treating it like a JSON object, I treated it like a string. I think that's where my problem lies, within the data sent, but I can't quite figure out where and why. – Vincent Aug 19 '22 at 21:02
  • 1
    You are missing the wrapping object – Daniel A. White Aug 19 '22 at 21:03
  • 2
    You also shouldn’t call result to block async – Daniel A. White Aug 19 '22 at 21:04
  • @mathis1337 the API endpoint is looking for a `PUT` request, I'm trying to update the item in the database. I think the issue lies with the data I'm sending it, but I can't see where or how – Vincent Aug 19 '22 at 21:06
  • Look at what json string you are sending – Daniel A. White Aug 19 '22 at 21:09
  • @DanielA.White can you add a bit more, I'm confused by what you meant wrapping object – Vincent Aug 19 '22 at 21:12
  • @DanielA.White the JSON string, or content being sent, looks like the following: `{"Item":{"Errors":null,"ID":"8c785cd5-673a-4b3c-b97d-b8446dad401a","Name":"Test2","OwnerID":"00000000-0000-0000-0000-000000000000"}}` – Vincent Aug 19 '22 at 21:17
  • `.Result` is wrong and can cause a deadlock, you should use `await` instead. And you are missing `using` on a few objects, and you should cache the `HttpClient` or you might get socket exhaustion. – Charlieface Aug 21 '22 at 09:18

1 Answers1

1

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"
    }
};
DMendoza
  • 412
  • 4
  • 8