Restsharp: WIE MAN: Senden Sie einen HTTP-Post für ein komplexes Objekt mit RestSharp

Erstellt am 4. Okt. 2013  ·  3Kommentare  ·  Quelle: restsharp/RestSharp

Könnte jemand einen Rat geben, wie man einen HTTP-Post mit den folgenden Anforderungsparametern sendet.

{
    "key": "example key",
    "message": {
        "html": "<p>Example HTML content</p>",
        "text": "Example text content",
        "subject": "example subject",
        "from_email": "[email protected]",
        "from_name": "Example Name",
        "to": [
            {
                "email": "[email protected]",
                "name": "Recipient Name"
            }
        ],
        "headers": {
            "Reply-To": "[email protected]"
        },
        "important": false,
        "track_opens": null,
        "track_clicks": null,
        "auto_text": null,
        "auto_html": null,
        "inline_css": null,
        "url_strip_qs": null,
        "preserve_recipients": null,
        "view_content_link": null,
        "bcc_address": "[email protected]",
        "tracking_domain": null,
        "signing_domain": null,
        "return_path_domain": null,
        "merge": true,
        "global_merge_vars": [
            {
                "name": "merge1",
                "content": "merge1 content"
            }
        ],
        "merge_vars": [
            {
                "rcpt": "[email protected]",
                "vars": [
                    {
                        "name": "merge2",
                        "content": "merge2 content"
                    }
                ]
            }
        ],
        "tags": [
            "password-resets"
        ],
        "subaccount": "customer-123",
        "google_analytics_domains": [
            "example.com"
        ],
        "google_analytics_campaign": "[email protected]",
        "metadata": {
            "website": "www.example.com"
        },
        "recipient_metadata": [
            {
                "rcpt": "[email protected]",
                "values": {
                    "user_id": 123456
                }
            }
        ],
        "attachments": [
            {
                "type": "text/plain",
                "name": "myfile.txt",
                "content": "ZXhhbXBsZSBmaWxl"
            }
        ],
        "images": [
            {
                "type": "image/png",
                "name": "IMAGECID",
                "content": "ZXhhbXBsZSBmaWxl"
            }
        ]
    },
    "async": false,
    "ip_pool": "Main Pool",
    "send_at": "example send_at"
}

Ich bin mir nicht sicher, wie ich das mit RestSharp machen soll

Hilfreichster Kommentar

Hallo,

1) Übersetzen Sie Ihren JSON in POCO (http://json2csharp.com/ eignet sich hervorragend dafür).
2)
var yourobject = new MyObject{.....................};
var json = JsonConvert.SerializeObject(deinObjekt);

var client = new RestClient(url);

var request = new RestRequest (actionPath, Method.POST);
Anfrage .AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
Anfrage .RequestFormat = DataFormat.Json;

_client.ExecuteAsync (restRequest, Antwort => ........);

Alle 3 Kommentare

Hallo,

1) Übersetzen Sie Ihren JSON in POCO (http://json2csharp.com/ eignet sich hervorragend dafür).
2)
var yourobject = new MyObject{.....................};
var json = JsonConvert.SerializeObject(deinObjekt);

var client = new RestClient(url);

var request = new RestRequest (actionPath, Method.POST);
Anfrage .AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
Anfrage .RequestFormat = DataFormat.Json;

_client.ExecuteAsync (restRequest, Antwort => ........);

tks mich auch!

Für die Nachwelt hat RestSharp dafür jetzt eingebaute Methoden.
Sehen Sie sich die Methode AddJsonBody() von RestRequest an
Dies ist ohnehin ab RestSharp 105.0.1.0 korrekt.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen

Verwandte Themen

stricq picture stricq  ·  6Kommentare

vDeggial picture vDeggial  ·  6Kommentare

nilesh-shah picture nilesh-shah  ·  6Kommentare

qJake picture qJake  ·  7Kommentare

thomasd3 picture thomasd3  ·  5Kommentare