-1

I have a class Contacts.cs which contains class ContactsDTO, as shown in the below code

namespace WindowsScheduling
{
    public class ContactsDTO    
     {
        public string ContactFirstName { get; set; }
        public string ContactLastName { get; set; }
        public string ContactAddress1 { get; set; }
        public string Class { get; set; }
     }
}

Now I want to send an object List<ContactsDTO> to an another project through REST API.

The method which I have implemented for this purpose is :-

 public string SendContactToKentico( List<ContactsDTO> objDeserializedMessage)
        {
            var RemoteURL = ConfigurationManager.AppSettings["RemoteURL"].ToString();     
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(RemoteURL);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage responseMessage = client.GetAsync(RemoteURL + "/Schedule/GetContactsByScheduler",objDeserializedMessage).Result;
            return msg;
        }

But here my objectDeserializedobject is showing an error :- Cannot convert from 'System.Collection.Generic.List' to 'System.Net.Http.HttpCompletionOption'

Rajat
  • 141
  • 1
  • 1
  • 7
  • @mjwills I have to use POST and I apologize for this mistake, I have replaced GetAsync by PostAsync but still I am receiving the same error. – Rajat Jun 12 '18 at 06:04
  • Please update your post to include your actual code. – mjwills Jun 12 '18 at 06:09

2 Answers2

1

You can't send a body with a GET request. Make sure to read the documentation for the classes you are using. The error message is telling you that none of the overloads for GetAsync take an argument representing body data. Choose the appropriate http verb for sending content; probably POST.

Tom W
  • 5,108
  • 4
  • 30
  • 52
  • Thanks for your reply, I have replaced GetAtsync with PostAsync but I am still receiving the same error. – Rajat Jun 12 '18 at 06:05
  • As Tom said, you need to read the documents to figure out which argument goes into which parameter, and what types the parameter will accept. It won't accept a `List<>`. You will need to convert the list into something that the method can accept, i.e. a class that derives from [HttpContent](https://msdn.microsoft.com/en-us/library/system.net.http.httpcontent(v=vs.118).aspx). See [this answer](https://stackoverflow.com/questions/23585919/send-json-via-post-in-c-sharp-and-receive-the-json-returned) for an example of how to send JSON as a StringContent. – John Wu Jun 12 '18 at 06:40
0

You could probably try something like this...

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(RemoteURL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string url = $"endPoint";
ObjectContent content = new ObjectContent<List<ContactsDTO>>(objMessage, new JsonMediaTypeFormatter());
HttpResponseMessage response = await client.PostAsync(url, content);

Please note: you may not need to use objDeserializedMessage, and you could just use objMessage. And also, I have done PostAsync opposed to GetAsync.

Also, do you want to make a GET request or a POST?

UPDATE 1 : Also check the response's status code like below

if(response.StatusCode == HttpStatusCode.OK){
    // handle the response
    ExpectedResponseModel responseModel = await response.Content.ReadAsAsync<ExpectedResponseModel >();
}
else {
    // request failed, handle error
}

Here, ExpectedResponseModel could be made of the response you're expecting.

AD8
  • 2,168
  • 2
  • 16
  • 31