Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOW TO: Send a HTTP Post for a complex object using RestSharp #435

Closed
abishekrsrikaanth opened this issue Oct 4, 2013 · 3 comments
Closed

Comments

@abishekrsrikaanth
Copy link

Could someone advice on how to send a HTTP Post with the below as the request parameters.

{
    "key": "example key",
    "message": {
        "html": "<p>Example HTML content</p>",
        "text": "Example text content",
        "subject": "example subject",
        "from_email": "message.from_email@example.com",
        "from_name": "Example Name",
        "to": [
            {
                "email": "recipient.email@example.com",
                "name": "Recipient Name"
            }
        ],
        "headers": {
            "Reply-To": "message.reply@example.com"
        },
        "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": "message.bcc_address@example.com",
        "tracking_domain": null,
        "signing_domain": null,
        "return_path_domain": null,
        "merge": true,
        "global_merge_vars": [
            {
                "name": "merge1",
                "content": "merge1 content"
            }
        ],
        "merge_vars": [
            {
                "rcpt": "recipient.email@example.com",
                "vars": [
                    {
                        "name": "merge2",
                        "content": "merge2 content"
                    }
                ]
            }
        ],
        "tags": [
            "password-resets"
        ],
        "subaccount": "customer-123",
        "google_analytics_domains": [
            "example.com"
        ],
        "google_analytics_campaign": "message.from_email@example.com",
        "metadata": {
            "website": "www.example.com"
        },
        "recipient_metadata": [
            {
                "rcpt": "recipient.email@example.com",
                "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"
}

I am not sure how to do this using RestSharp

@claudiosanchez
Copy link

Hi,

  1. Translate your JSON to POCO (http://json2csharp.com/ is great for that).

var yourobject = new MyObject{.....................};
var json = JsonConvert.SerializeObject(yourobject);

var client = new RestClient(url);

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

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

@hallem hallem closed this as completed Oct 15, 2014
@rodrigoporcionato
Copy link

tks me too!

@Caltor
Copy link

Caltor commented Sep 20, 2018

For posterity RestSharp has inbuilt methods for this now.
Check out the AddJsonBody() method of RestRequest
This is correct for RestSharp 105.0.1.0 onwards anyhow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants