Search code examples
graphqlpostgraphile

Postgraphile StatusCode: 405, ReasonPhrase: 'Method Not Allowed


I use Postgraphile locally and it work very well.

enter image description here

I want to send a HttpClient post requset in my Application, but it does not work and I get this error:

StatusCode: 405, ReasonPhrase: 'Method Not Allowed

hier is my code:

 using (HttpClient httpClient = new HttpClient())
    {
        string content = "query {accounts {nodes {id,name,street,postalcode,city}}}";
        var httpConent = new StringContent(content, Encoding.UTF8, "application/json");
        var responseMessage = await httpClient.PostAsync("http://localhost:5000/graphiql", httpConent);
        var result = responseMessage.Content.ReadAsStringAsync();
    }

Solution

  • In line 5 of your code snippet, you're submitting to the /graphiql URL (which is for the GraphiQL GUI), you should be submitting to /graphql.

    In line 4 of your snippet, you are claiming the content variable is application/json, but it is in fact a GraphQL string. You should be submitting something like {"query":"{__typename}"} as application/json.

    You do not appear to be issuing an Accept: application/json header.

    I suggest you use the network debugging tools of your web browser to inspect exactly what the browser is doing when it runs the GraphQL query, and compare that with what you are attempting to do with your code. You might also refer to the GraphQL-over-HTTP specification: https://graphql.github.io/graphql-over-http/draft/