Link to home
Start Free TrialLog in
Avatar of sbornstein2
sbornstein2

asked on

Consuming Web API in C# Help Need Guru Here

Hello all,

I have a Web API that I am trying to do the following.   I need to pass to it a MemoryStream and then some string params as follows:

In my controller I have:

  public HttpResponseMessage Post(SharepointParameter spParameter)
        {
            var response = SharepointService.UploadSharepointDocument(spParameter.File, spParameter.SitePath, spParameter.FileName);

            return new HttpResponseMessage(HttpStatusCode.Created);
        }

I then have this parameter class defined:

public class SharepointParameter
    {
        public SharepointParameter() { }
        public SharepointParameter(MemoryStream File, string SitePath, string FileName)
        {
            this.File = File;
            this.SitePath = SitePath;
            this.FileName = FileName;
        }

        public MemoryStream File { get; set; }
        public string SitePath { get; set; }
        public string FileName { get; set; }
    }

Something similar I did but without a stream just string and ints I then used Fiddler and the POST under composer I used the URL Route such as:

http://localhost:62011/api/Sharepoint/Sharepoint

User-Agent: Fiddler
Content-Type: application/json
Host: localhost:62011
Content-Length: 83

and in Request Body passed the parameter this is from another app I have:
{
"Mode":"S",
"BillingGroupId":9,
"EndDate":"6/30/2014",
"InvoiceTypeId":1
}

Now I need to consume the sharepoint Web API I am building from an MVC application.   So far I have this in my actionresult in the MVC app:
  PdfGenerate pdf = new PdfGenerate();
            MemoryStream ms = new MemoryStream();
            ms = pdf.CreatePdf(id);

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(WebConfigurationManager.AppSettings["SharepointWebAPIPath"]);
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

So I have my Memory stream so now I am not sure how I pass the MemoryStream and two strings SitePath and FileName along with it.   Can the stream also serialize in JSON etc?  The file will be a PDF file.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can't you get the bytes from memory stream and pass it to your web api ? Check in this link where they are posting a memory stream in web request, http://www.andrescottwilson.com/posting-content-from-memory-stream-using-httpwebrequest-c/. Hope it helps.
Avatar of sbornstein2
sbornstein2

ASKER

tx