Search code examples
c#postmanups

Sending JSON from Postman returns code 200, Sending JSON from HttpClient returns 503 (Your chunked POST data is too large to upload)


I'm making an API call to UPS using their Address Validation call (https://onlinetools.ups.com/rest/AV). I have had success calling different Gets from UPS before(example: UPS Address Street Level Validation).

Here is the JSON that is being sent:

 {
    "AccessRequest": {
        "AccessLicenseNumber": "removed for security reasons",
        "UserId": "removed for security reasons",
        "Password": "removed for security reasons"
    },
    "AddressValidationRequest": {
        "Request": {
            "TransactionReference": {
                "CustomerContext": ""
            },
            "RequestAction": "AV"
        },
        "Address": {
            "City": "",
            "StateProvinceCode": "",
            "PostalCode": "98272"
        }
    }
}

In PostMan, there are only the default Headers being used:

enter image description here

This returns successfully with the data I am looking for with code: 200.

In my .Net Standard 2.0 Library, I am using HTTPClient(Microsoft.AspNet.WebApi.Client 5.2.7) to make a call to the API. Here is how I initiate the HTTP Client(variable called "ApiClient"):

string api = _config["UPS_API_LINK"];
_apiClient = new HttpClient();
_apiClient.BaseAddress = new Uri(api);
_apiClient.DefaultRequestHeaders.Accept.Clear();
_apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Now I have created the models to line up so it sends the exact same JSON as Postman does. You will see with the code below that I double check that by serializing it myself (I take that string, copy it into postman, and works like a charm). So I send my request. I shortened the code a little bit make my point:

string json = JsonConvert.SerializeObject(request);
Debug.Print(json);
using (HttpResponseMessage response = await ApiClient.PostAsJsonAsync("/rest/AV", request))
{
    var content = await response.Content.ReadAsStringAsync();
}

Here is what the content returns:

<HTML><HEAD><TITLE>Weblogic Bridge Message</TITLE></HEAD> <BODY><H2>Failure of Web Server bridge:</H2><P><hr>Your chunked POST data is too large to upload.<hr> </BODY></HTML>

The return code is 503. So can someone lead me into the right direction? I just don't know what HTTP Client is sending that is making it so large that I get this response. Whats surprising to me is that I have another call to the api that sends more data and it works perfectly. Any suggestions would be appreciated.

Note: I have double checked that I am requesting the call using the right URL

enter image description here

Update: Thanks to Nehorai, I discovered that I can use third party dll called RestSharp for making API calls. This library worked flawlessly! Though I would love to still know why HTTP Client does not work in this situation. Here is my code below using RestSharp incase anyone wanted to see it:

var client = new RestClient("https://onlinetools.ups.com/rest/AV");
client.Timeout = -1;
var requestRest = new RestRequest(Method.POST);
requestRest.AddHeader("Content-Type", "application/json");
requestRest.AddJsonBody(request);
var result = await client.ExecutePostAsync<ZipInfo_RootResponse>(requestRest);

Solution

  • In postman, you can view a code snippet of the request, click on "Code":

    enter image description here

    Then you can select the language, select C# and you can see the request in C# code (you will need to add a reference to RestSharp dll)