I’m trying to integrate a shipping API into my C# project to get AWB numbers when orders are placed. The API works fine in Postman, but I’m getting a null response when using the VerveLogic.HttpHandler package in my code.
Here’s what I’ve tried:
var client = new HttpHandler.Client("https://api.shippingservice.com/v2/awb");
var requestUrl = "https://api.shippingservice.com/v2/awb?user=myuser&pass=mypass&num=1&shipType=prepaid";
var response = client.PostData<dynamic>(requestUrl, new { user = "myuser", pass = "mypass", num = 1, shipType = "prepaid" });
if (response != null) {
// Process response
}
The API documentation says to use a POST request with these parameters, but I’m not sure if I’m formatting the request correctly in C#. Has anyone successfully integrated this kind of shipping API using HttpHandler or another C# method? Any tips on debugging why I’m getting a null response would be really helpful. Thanks!
Hey there Liam_Stardust! I’ve been tinkering with shipping APIs too, and they can be tricky beasts. Have you considered using Flurl? It’s my go-to for HTTP calls these days - super clean and easy to use.
Something like this might do the trick:
var response = await "https://api.shippingservice.com/v2/awb"
.PostJsonAsync(new { user = "myuser", pass = "mypass", num = 1, shipType = "prepaid" })
.ReceiveJson<dynamic>();
Just make sure you’ve got the Flurl.Http NuGet package installed.
Also, have you checked the API docs for any quirks? Sometimes they want weird stuff like specific user agents or custom headers. Maybe fire up Fiddler and compare your C# request with the Postman one?
Let us know how it goes! Curious to see what solution works for you in the end.
have u tried using RestSharp? its pretty good for API calls. i had similar issues and switching to RestSharp solved em. heres a quick example:
var client = new RestClient("https://api.shippingservice.com/v2/");
var request = new RestRequest("awb", Method.POST);
request.AddParameter("user", "myuser");
// add other params
var response = client.Execute(request);
hope this helps!
I’ve encountered similar issues when integrating shipping APIs. One thing to check is if the API requires specific headers. Try adding headers to your request, especially Content-Type. Also, ensure you’re using HTTPS if required.
Another approach is to use HttpClient instead of HttpHandler. It’s more flexible and widely supported. Here’s a basic example:
using (var client = new HttpClient())
{
var content = new StringContent(JsonConvert.SerializeObject(new { user = "myuser", pass = "mypass", num = 1, shipType = "prepaid" }), Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://api.shippingservice.com/v2/awb", content);
var responseString = await response.Content.ReadAsStringAsync();
// Process responseString
}
If you still get null responses, try logging the full response, including status codes and headers. This can provide valuable debugging information.