Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am doing c# web application for that I am using Rest api. I tried to pass url to the web api to get the response in that response I have the content of xml data. I am able to receive that, but when tried to send that data to the server side to copy that data using PostAsync I am getting problem can any one help me I am trying from many days but I am unable to fix this problem.

please go through my code and give me the response. Thanks in advance.

What I have tried:

C#
<blockquote class="quote"><div class="op">Quote:</div>Here is my Client side code I am sending request.


 urlstring = @"api/home/CopyHotFolders?folderName=new&virtualPrName=FieryDemo print&isHotFolderEnabled=true";
   
    var response = client.GetAsync(urlstring).Result;
    if (response.IsSuccessStatusCode)
    {
        string data = await response.Content.ReadAsStringAsync();
    }


 here I am getting response "data" . that data I am trying to send request to the server  using below code. 


StringContent httpContent = new StringContent(data, Encoding.UTF8, "text/json");

var serverAddress2 = string.Format(System.Globalization.CultureInfo.InvariantCulture,
"http://{0}/", serverIP + ":5555");

using (var client2 = new HttpClient())
{
    client2.BaseAddress = new Uri(serverAddress2);
    urlstring = @"api/home/CopyHotFolderToDestination?folderName=xyz&virtualPrName=FieryDemo print&IsDefault=true";
    
    HttpResponseMessage response2 = await client2.PostAsync(new Uri(urlstring, UriKind.Relative), httpContent);
    
    if (response2.IsSuccessStatusCode)
    {
        string data2 = await response2.Content.ReadAsStringAsync();
    } 
    
           

// ...web api is like this...........


[AcceptVerbs("POST")]
[HttpPost]
[Route("api/home/{CopyHotFolderToDestination}")]

public HttpResponseMessage CopyHotFolderToDestination(string folderName, string virtualPrName, bool IsDefault)
{
    string xmlData;
    
    HttpResponse request = HttpContext.Current.Response;
    string httpContent =  Request.Content.ReadAsStringAsync().Result;
    
    xmlData = request.ToString();
    StringContent xmlvalue = new StringContent(JsonConvert.SerializeObject(xmlData), Encoding.UTF8, "application/json");
    return new HttpResponseMessage()
    {
    Content = xmlvalue
    };                
}

I am getting response like this ."\"System.Web.HttpResponse\"" instead of my xmldata.
I need my xml data which I am sending to the server.</blockquote>
Posted
Updated 16-Jul-18 23:38pm
v2
Comments
F-ES Sitecore 17-Jul-18 5:57am    
request.ToString()

that line is going to return the name of the request type which is where System.Web.HttpResponse is coming from. Looks like you're getting the request and response mixed up. Just google for examples of how to use webapi and that will show you how to read their parameters.
Member 13914613 17-Jul-18 8:42am    
thank you for your response.

I did small change its working fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900