0

I am trying to get postcard information from LOB.com account using CURL.

lob.com they are providing:

curl https://api.lob.com/v1/postcards/psc_5c002b86ce47537a \
  -u test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc:

How can I get the json response using this CURL?

I've used

var httpWebRequest = (HttpWebRequest) WebRequest.Create("https://api.lob.com/v1/postcards/psc_95d1eafeb5b9a766");

httpWebRequest.ContentType = "application/json";

httpWebRequest.Accept = "*/*";

httpWebRequest.Method = "GET";

httpWebRequest.Headers.Add("Authorization", "test_48d2bbf75c2edc75155c401d119bfae5526:");

var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();

using(var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
 gta_allCustomersResponse answer = JsonConvert.DeserializeObject < gta_allCustomersResponse > (streamReader.ReadToEnd());
 return answer;

Above code showing 422 (Unprocessable entity) or 400 (Bad request) error if I run this code.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
saravanan049
  • 60
  • 10
  • What language is this? – SIGSTACKFAULT Apr 11 '18 at 13:25
  • C# I guess... based on the title too. – Francesco B. Apr 11 '18 at 13:35
  • Is this answer resolved your issue? – Divyang Desai Apr 13 '18 at 16:59
  • 1
    before i got ur answer i got what i expected ... that too the same like ur answer.. your answer is also very useful.. thanks @div – saravanan049 Apr 15 '18 at 05:22
  • @saravanan049: And where did you get it from? c-sharp corner? :) – Divyang Desai Apr 15 '18 at 05:24
  • no.. somewhere here in stackoverflow..StringBuilder postdata = new StringBuilder(); foreach (KeyValuePair item in zDicPageData) { postdata.AppendFormat("&{0}={1}", item.Key, item.Value); } using (Stream writeStream = zHttpWebRequest.GetRequestStream()) { UTF8Encoding encoding = new UTF8Encoding(); byte[] bytes = encoding.GetBytes(postdata.ToString()); writeStream.Write(bytes, 0, bytes.Length); } – saravanan049 Apr 15 '18 at 05:32
  • like the above ... i used to convert dictionary data to HTTPRequest... more or less same like ur code... but.. very much thanks for ur valuable answer.. – saravanan049 Apr 15 '18 at 05:33
  • @saravanan049: Glad to know that your issue has been resolved and I could help you anyway. Do not forgot to up-vote and accept the answer!! :) – Divyang Desai Apr 23 '18 at 02:53

1 Answers1

0

I recommend using RestSharp to send simple HTTP requests to Lob.

Or you can make it using HttpClient:

public static void Main()
{
    string API_KEY = "YourApiKey";

    HttpClient httpClient = new HttpClient();
    var authHeaderBytes = Encoding.ASCII.GetBytes(API_KEY + ":");
    httpClient.DefaultRequestHeaders.Authorization = 
        new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authHeaderBytes));

    var requestPayload = new Dictionary<string, string>
    {
        { "name", "John Doe" },
        { "address_line1", "123 Main St" },
        { "address_city", "San Francisco" },
        { "address_state", "CA" },
        { "address_zip", "94107" },
        { "address_country", "US" }
    };

    var encodedRequestForm = new FormUrlEncodedContent(requestPayload);
    var APIResponse = httpClient.PostAsync("https://api.lob.com/v1/addresses", encodedRequestForm).GetAwaiter().GetResult();
    var addressString = APIResponse.Content.ReadAsStringAsync().GetAwaiter().GetResult();

    System.Console.WriteLine(addressString);
}

This example is for an address, you can develop same for postcards, just review the curl request or Java request from the documentation and code accordingly.

Hope this helps!

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76