4
In the example below, I need to pass a login pair/password, because the REST service requires authentication (Basic Authentication). So how should I pass this information in the section below? (Additional information: authentication must be done encoded in the upload header)
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://dominio:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// New code:
HttpResponseMessage response = await client.GetAsync("api/products/1");
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync>Product>();
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
It depends on the implementation of the REST service you are trying to use. Some ask for the API key as an optional parameter, others ask for a password/login pair in the headers. View the API documentation and change the question.
– Rodrigo Rigotti
Dear Rodrigo, I did as oriented. It is not optional, it should be covered in Header. All the API documentation is geared towards JAVA, I’m having a hard time with this. Thank you.
– SantanaFire