Restsharp: ๋ฐฉ๋ฒ•: RestSharp๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณต์žกํ•œ ๊ฐœ์ฒด์— ๋Œ€ํ•œ HTTP ๊ฒŒ์‹œ๋ฌผ ๋ณด๋‚ด๊ธฐ

์— ๋งŒ๋“  2013๋…„ 10์›” 04์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: restsharp/RestSharp

๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์•„๋ž˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ HTTP Post๋ฅผ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์กฐ์–ธํ•ด ์ฃผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?

{
    "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"
}

RestSharp๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์•ˆ๋…•,

1) JSON์„ POCO๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค(http://json2csharp.com/์ด ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค).
2)
var yourobject = new MyObject{...........};
var json = JsonConvert.SerializeObject(๊ท€ํ•˜์˜ ๊ฐœ์ฒด);

var ํด๋ผ์ด์–ธํŠธ = ์ƒˆ๋กœ์šด RestClient(url);

var ์š”์ฒญ = ์ƒˆ๋กœ์šด RestRequest(actionPath, Method.POST);
์š”์ฒญ .AddParameter("์‘์šฉ ํ”„๋กœ๊ทธ๋žจ/json; charset=utf-8", json, ParameterType.RequestBody);
์š”์ฒญ .RequestFormat = DataFormat.Json;

_client.ExecuteAsync(restRequest, ์‘๋‹ต => ........);

๋ชจ๋“  3 ๋Œ“๊ธ€

์•ˆ๋…•,

1) JSON์„ POCO๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค(http://json2csharp.com/์ด ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค).
2)
var yourobject = new MyObject{...........};
var json = JsonConvert.SerializeObject(๊ท€ํ•˜์˜ ๊ฐœ์ฒด);

var ํด๋ผ์ด์–ธํŠธ = ์ƒˆ๋กœ์šด RestClient(url);

var ์š”์ฒญ = ์ƒˆ๋กœ์šด RestRequest(actionPath, Method.POST);
์š”์ฒญ .AddParameter("์‘์šฉ ํ”„๋กœ๊ทธ๋žจ/json; charset=utf-8", json, ParameterType.RequestBody);
์š”์ฒญ .RequestFormat = DataFormat.Json;

_client.ExecuteAsync(restRequest, ์‘๋‹ต => ........);

๋‚˜๋„ ๊ทธ๋ž˜!

ํ›„์†์„ ์œ„ํ•ด RestSharp์—๋Š” ํ˜„์žฌ ์ด๋ฅผ ์œ„ํ•œ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
RestRequest ์˜ AddJsonBody() ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.
์–ด์จŒ๋“  ์ด๊ฒƒ์€ RestSharp 105.0.1.0 ์ด์ƒ์—์„œ ์ •ํ™•ํ•ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰