POST JSON with Flurl

送分小仙女□ 提交于 2019-12-22 13:49:12

问题


I start with Flurl and I would like to create a POST but I think I have a problem with the format of my JSON parameters.

You can see the JSON parameters:

{
    "aaaUser" : {
    "attributes" : {
        "name" : "device:domain\\login",
        "pwd" : "123456"
        }
    }
}

These settings work with Postman and now I would like to use Flurl to continue my little POST :) But my JSON format is not correct.

using System.Threading.Tasks;
using Flurl.Http;

namespace Script
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var result = await "https://IP/api/aaaLogin.json".PostUrlEncodedAsync(new
            {
                name = "device:domain\\login",
                pwd = "123456"
            });
        }
    }
}

Thank you for your help !


回答1:


I think 2 problems have been identified here.

  1. You're using PostUrlEncodedAsync, which is going to send the data in URL-encoded format, like this: name=device:domain\\login&pwd=123456. If you want the data serialized to JSON, use PostJsonAsync instead.

  2. You're only including the nested attributes object of the JSON and not the entire object.

In short, you're going to want something like this:

var result = await "https://IP/api/aaaLogin.json".PostJsonAsync(new
{
    aaaUser = new
    {
        attributes = new
        {
            name = "device:domain\\login",
            pwd = "123456"
        }
    }
});

Once you get this far, you're going to need to know how to process the results. If the response is JSON formatted, you'll likely want to append .ReceiveJson() or .ReceiveJson<T>() to the above call in order to have a more friendly object to work with. Please refer to the documentation.



来源:https://stackoverflow.com/questions/53778304/post-json-with-flurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!