问题
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.
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, usePostJsonAsync
instead.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