Trouble fetching response from shipping API using C# HttpHandler

Hey guys, I’m trying to set up a shipping integration for my online store. I need to get AWB numbers when orders are placed. The API works fine in Postman, but I’m having issues with my C# code.

Here’s what I’ve tried:

var apiClient = new ShippingHandler.ApiClient("https://api.shippingprovider.com/v2/get_awb/");
var requestUrl = "https://api.shippingprovider.com/v2/get_awb/?user=myuser&pass=mypass&qty=1&shipType=prepaid";
var result = apiClient.SendRequest<object>(requestUrl, new { user = "myuser", pass = "mypass", qty = 1, shipType = "prepaid" });
if (result != null) {
    // Process the result
}

The code returns null, even though the API works in Postman. I’m using a NuGet package for HTTP requests. Any ideas on how to fix this or a better way to get the AWB number using C#? Thanks in advance!

Hey there WittyCodr99! :slightly_smiling_face:

Hmm, that’s a tricky one. Have you tried using HttpClient directly instead of a custom ApiClient? Sometimes simpler is better! Here’s a quick idea:

using var client = new HttpClient();
var response = await client.GetAsync(requestUrl);

if (response.IsSuccessStatusCode)
{
    var result = await response.Content.ReadAsStringAsync();
    // Now you can work with 'result'
}

Oh, and don’t forget to check if your API key or credentials are correct. Maybe they’re different in your code vs Postman?

Also, what does the response look like when it fails? Any error messages? That could give us more clues.

Let us know how it goes! Shipping integrations can be such a pain sometimes, right? :sweat_smile:

hey wittycodr99, try using httpclient. maybe this code works:

using var client = new HttpClient();
var response = await client.GetAsync(requestUrl);
var result = await response.Content.ReadAsStringAsync();

this might solve ur issue. also, check ur api credentials.

Have you considered using RestSharp? It’s a popular library for making HTTP requests in C# and might be easier to work with than HttpClient. Here’s a quick example:

var client = new RestClient("https://api.shippingprovider.com/v2");
var request = new RestRequest("get_awb", Method.Get);
request.AddParameter("user", "myuser");
request.AddParameter("pass", "mypass");
request.AddParameter("qty", 1);
request.AddParameter("shipType", "prepaid");
var response = client.Execute(request);

if (response.IsSuccessful)
{
    var result = JsonConvert.DeserializeObject<YourResponseType>(response.Content);
    // Process the result
}

This approach gives you more control over the request and response handling. Also, make sure you’re handling any potential exceptions that might occur during the API call.