Postgraphile 状态代码: 405, 原因短语: ‘方法不允许’

huangapple go评论61阅读模式
英文:

Postgraphile StatusCode: 405, ReasonPhrase: 'Method Not Allowed

问题

我在本地使用Postgraphile,它运行得非常好。

我想在我的应用程序中发送一个HttpClient post请求,但它不起作用,我得到了这个错误:

StatusCode: 405, ReasonPhrase: 'Method Not Allowed'

这是我的代码:

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();
}
英文:

I use Postgraphile locally and it work very well.

Postgraphile 状态代码: 405, 原因短语: ‘方法不允许’

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();
    }

答案1

得分: 1

在你的代码片段的第5行,你正在提交到/graphiql URL(这是用于GraphiQL GUI的),你应该提交到/graphql

在你的片段的第4行,你声称content变量是application/json,但实际上它是一个GraphQL字符串。你应该提交类似{"query":"{__typename}"}这样的内容作为application/json

你似乎没有发送Accept: application/json头。

我建议你使用你的网页浏览器的网络调试工具来检查当运行GraphQL查询时浏览器的确切行为,并将其与你的代码尝试做的进行比较。你也可以参考GraphQL-over-HTTP规范:https://graphql.github.io/graphql-over-http/draft/

英文:

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/

huangapple
  • 本文由 发表于 2023年2月18日 00:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定