0

Attempting to post to api.lob.com using HTTPClient. During debug intellisense shows a value in the HTTPRequestMessage, however, the task httpresponsemessage value shows nothing at all.

Built the code based on this post click here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Formatting;

namespace HTTPClientAPICall
{
    class Program
    {
        static void Main(string[] args)
        {

            callAPI();  

        }

    static void callAPI()
    {
        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("https://api.lob.com");

        client.DefaultRequestHeaders
            .Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new 

AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "test_xxxxxxxxxxxxxxxxxxxxxxxxxx", ""))));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/v1/verify");

            request.Content = new StringContent("{\"address_line1\":\"1600 Pennsylvania Ave NW\",\"address_city\":\"Washington\",\"address_state\":\"DC\",\"address_zip\":\"20500\"}", Encoding.UTF8, "application/json");

            client.SendAsync(request).ContinueWith

                ((responseTask) =>
                  {

                      Console.WriteLine("Response: {0}", responseTask.Result);

                  });

        }
    }
}

Intellisense enter image description here

vs.

enter image description here

I have looked at using RESTSharp but would prefer to use straight C# without the extra references.

Community
  • 1
  • 1
stroopwafel
  • 151
  • 1
  • 2
  • 12

1 Answers1

0

Made several mistakes in the original code. This solution is working. Used this post to identify the starting point for the ultimate solution: click here

The key to the solution was JSON.NET.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Configuration;
using Newtonsoft.Json;

namespace JSONandHTTPClient
{
    class Program
    {
        static void Main(string[] args)
        {

            callJSONandHTTPClientApp();

        }

        static void callJSONandHTTPClientApp()
        {
            using (var client = new HttpClient())
            {
                var parameters = new Dictionary<string, string>();
                parameters["address_line1"] = "1600 Pennsylvania Ave NW";
                parameters["address_city"] = "Washington";
                parameters["address_state"] = "DC";
                parameters["address_zip"] = "20050";

                client.BaseAddress = new Uri("https://api.lob.com");

                client.DefaultRequestHeaders
                .Accept
                .Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", ""))));

                var justJSON = JsonConvert.SerializeObject(parameters).ToString();

                var value = new StringContent(justJSON, Encoding.UTF8, "application/json");

                var response = client.PostAsync("v1/verify", value).Result;

                if (response.IsSuccessStatusCode)
                {
                    dynamic content = JsonConvert.DeserializeObject(
                        response.Content.ReadAsStringAsync()
                        .Result);

                    Console.WriteLine(content.ToString());
                }

                Console.ReadLine();
            }

        }
    }
}
stroopwafel
  • 151
  • 1
  • 2
  • 12