简体   繁体   中英

How to make a POST to Eloqua by rest api?

I am working with eloqua 10.

I need to create Email in eloqua by using their rest api from an aspx web page.But am get 404 Bad Request .

Given below is the code sample for the POST request that I tried.

string authenticateStr = eloquainstance + @"\" + username + ':' + password;

byte[] bytesToEncode = Encoding.UTF8.GetBytes(authenticateStr);

string encodedText = Convert.ToBase64String(bytesToEncode);

string requrl = "/assets/email";

string requestBody = "<subject>Test subject</subject>" +
                        "<senderName>Ajai Test</senderName>" +
                        "<senderEmail>amani@suyati.com</senderEmail>" +
                        "<emailGroupId>9</emailGroupId>" +
                        "<htmlContent>This is a test email templete created     trough rest api.This is for testing purpose only</htmlContent>"+
                        "<type>Email</type>" +
                        "<name>Email By api</name>";


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseurl + requrl);
request.Headers.Add("Authorization", "Basic " + encodedText);
request.Accept = "application/xml";//"application/x-www-form-urlencoded";
request.Method = "POST";
request.ContentType = "application/xml";
request.ContentLength = requestBody.Length;

//write body to text
byte[] body = System.Text.Encoding.UTF8.GetBytes(requestBody);
Stream dataStream = request.GetRequestStream();
dataStream.Write(body, 0, requestBody.Length);
dataStream.Close();

HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

Please correct me if anything wrong in my code.

Can anybody tried a post using eloqua rest api, if so can you share a sample code to make a POST request to eloqua from c#.Any help be appreciable.

Not sure whether you've got this working yet as some time has passed since you asked this, but you can find a whole set of samples for using eloqua's topliners site:

http://topliners.eloqua.com/community/code_it/blog/2012/10/08/eloqua-rest-api--open-source-c-samples

Looking at your code I think you may need to escape the html body

I would also consider writing a class to represent the email object and then use restsharp to simplify the process of serialising the request and handling the response.

Also experiment with test requests using fiddler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM