简体   繁体   English

从C#到Flask REST API的HTTP Post请求不会附加任何参数?

[英]HTTP Post Request from C# to Flask REST API won't attach any parameters?

I have been attempting to do a simple POST request to a basic Flask API, and no matter what I try it simply will do http://localhost:5000/ rather than the http://localhost/?test_key=test_value that I want it to do. 我一直在尝试对基本的Flask API做一个简单的POST请求,无论我尝试什么,它都会执行http://localhost:5000/而不是我想要的http://localhost/?test_key=test_value它要做。 C# is not my strongest language, but I have tried a bunch of different methods that I found online. C#不是我最强的语言,但我尝试了一些我在网上找到的不同方法。 I don't think it has to do with my method being bad at this point, I more so think that I'm missing a major piece entirely. 我不认为这与我的方法在这一点上不好有关,我更认为我完全错过了一个重要的部分。

Here is the most recent attempt I tried (keep in mind this is a snippet of a far larger project, but it doesn't involve any other pieces of it): 这是我尝试过的最新尝试(请记住,这是一个更大的项目的片段,但它不涉及任何其他部分):

    class DisputeResponse
    {
        public int id;
        public string res;
        public string test_key;
    }

    [HttpPost]
    public async Task<JsonResult> TestResponse(int id, string res)
    {
        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("http://localhost:5000/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            var dispRes = new DisputeResponse();
            dispRes.test_key = "test_value";

            var result = await client.PostAsync("http://localhost:5000/",
                new StringContent(JsonConvert.SerializeObject(dispRes), Encoding.UTF8, "application/json"
                ));

            result.EnsureSuccessStatusCode();
        }
        catch (Exception e)
        {
            System.Diagnostics.Trace.TraceInformation(e.Message);
        }


        return null;
    }

Here is the output when I use Postman (works perfectly): 这是我使用Postman时的输出(完美地工作):
127.0.0.1 - - [15/Apr/2019 16:26:28] "POST /?test_key=test_value HTTP/1.1" 200 -

And here is when I try to use C# code: 这是我尝试使用C#代码的时候:
127.0.0.1 - - [15/Apr/2019 16:36:54] "POST / HTTP/1.1" 500 -

EDIT: Sorry I had some extraneous lines that I removed. 编辑:对不起,我删除了一些无关的行。

The HTTPClient is putting the parameters inside the body of the request. HTTPClient将参数放在请求正文中。 If you would like to add it to the URI you can do so by modifying the URI string. 如果要将其添加到URI,可以通过修改URI字符串来实现。 If you are not posting anything to the body, I would modify the FLASK API to receive a GET instead of a POST. 如果您没有向正文发布任何内容,我会修改FLASK API以接收GET而不是POST。

Typically I would only use query string parameters in the URI string for HTTPGet requests, even though there is no rule stopping you from doing this with a HTTPPost. 通常我只会在HTTP字符串请求的URI字符串中使用查询字符串参数,即使没有规则阻止您使用HTTPPost执行此操作。 Additionally, I would only include my parameters for an HTTPPost request within the body. 另外,我只会在正文中包含我的HTTPPost请求参数。 Again, no rule for this but just what I do. 再说一次,没有这个规则,只是我做的事情。

Here are a few examples to help you along with the HTTPClient post async method: 以下是一些帮助您使用HTTPClient post async方法的示例:

https://csharp.hotexamples.com/examples/-/HttpClient/PostAsync/php-httpclient-postasync-method-examples.html https://csharp.hotexamples.com/examples/-/HttpClient/PostAsync/php-httpclient-postasync-method-examples.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM