英文:
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.
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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论