locked
Method Not Allowed when use SendAsync() instead of GetAsync() RRS feed

  • Question

  • User204212 posted

    Hi guys, is me again.

    I have another interesting situation

    I need to send a Get request to an API but with content in the body, but I never do that previously (only with post request).

    ``` public async Task GetIdentificaciones() { try { var direURL = ApiSIF + "cuteURL"; var uri = new Uri(string.Format(direURL, string.Empty));

                var response = await common.SIFHttpClient().GetAsync(uri);
    
                if (response.IsSuccessStatusCode)
                {
                    var jstr = await response.Content.ReadAsStringAsync();
                    var identificaciones = JsonConvert.DeserializeObject<List<Identificacion>>(jstr);
    
                    return response.ReasonPhrase;
    
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error en la funcion para obtener Identificaciones", e.InnerException + e.Message);
            }
    
            return "BAD";
        }
    

    ```

    That's the normal code to make and Get request, work fine and return 200, very good, but when I try it this code to add content to the body in a GeT request: ``` ... var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri(ApiSIF + "cuteURL"), Content = new StringContent("83", Encoding.UTF8, "application/json") };

    var response = await common.SIFHttpClient().SendAsync(request);

    if (response.IsSuccessStatusCode) { ... ```

    It's send me a "Method Not Allowed" error code.

    Anybody knows how to make this kind of request?

    BTW: I'm not the dev for the API, so I cant modify anything in the API project.

    Wednesday, August 26, 2020 4:30 AM

All replies

  • User2148 posted

    Get should not have a body... you should use Post

    Wednesday, August 26, 2020 6:08 AM
  • User204212 posted

    @AlessandroCaliaro said: Get should not have a body... you should use Post

    Hi @AlessandroCaliaro I know that, but see, I dont dev the API, so I need to adjust to him.

    Wednesday, August 26, 2020 12:57 PM
  • User204212 posted

    @NMackay can u help me with that question? please :neutral:

    Friday, August 28, 2020 4:41 AM
  • User76049 posted

    Not really, API should be changed, unless you can pass whatever custom config through the headers, for Get you just supply a URL, Libraries like Refit will expect that behavior.

    Seen posts used in Restful API's that return a json response with data, sometimes for authentication etc but it's a weird API experience generally.

    Can't really add any more than the response from @AlessandroCaliaro

    You can do a post and return data though, it's not really a get or Restful though, get's are cached and optimised.

    Friday, August 28, 2020 3:56 PM
  • User204212 posted

    @NMackay said: Not really, API should be changed, unless you can pass whatever custom config through the headers, for Get you just supply a URL, Libraries like Refit will expect that behavior.

    Seen posts used in Restful API's that return a json response with data, sometimes for authentication etc but it's a weird API experience generally.

    Can't really add any more than the response from @AlessandroCaliaro

    You can do a post and return data though, it's not really a get or Restful though, get's are cached and optimised.

    Thanks, I will send this to the API dev to see if they can change that.

    Cheers!

    Friday, August 28, 2020 11:15 PM