Search code examples
c#asp.netjsonapihttpwebrequest

JSON HttpWebRequest always returns 400


I tried to make a request to test if people should be able to pay on invoice.

I've never made an API call with JSON data before so i read through some documentations and stack overflow questions and found this thread right here, which was pretty promissing: How to post JSON to a server using C#?

The code where i attempt to do this is the one posted below:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://service-zs.riskcube.ch/api/v1/RiskCube/claim");
        request.ContentType = "application/json";
        request.Method = "POST";
        request.Headers.Add("X-API-KEY", "123"); //Replace with the API KEY given from CreditForm
        if (creditReformModel != null)
        {
            string postData = JsonConvert.SerializeObject(creditReformModel);
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(postData);
            }
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

I tried to implement the answer to the question but sadly i always get a 400 bad request error when i try to send the httpRequest.

enter image description here

The error message says: System.Net.WebException: "The remoteserver responded with an error: (400) bad request"

Those are the details to the exception:

System.Net.WebException
  HResult=0x80131509
  Message = The remote server returned an error: (400) Invalid request.
  Source = <The exception source cannot be evaluated.>.
  Stack monitoring:
<The exception stack monitor cannot be evaluated.>.

That's the model i'm trying to serialize and add to the httpWebRequest:

    internal class CreditReformModel
    {
        public string ShopId { get; set; }
        public string OrderProcessId { get; set; }
        public string IpAddress { get; set; }
        public string MacAddress { get; set; }
        public string CustomerId { get; set; }
        public CreditReformAddressModel ShippingAddress { get; set; }
        public CreditReformAddressModel BillingAddress { get; set; }
    }

    internal class CreditReformAddressModel
    {
        public string Type { get; set; }
        public string BusinessName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Co { get; set; }
        public string Street { get; set; }
        public string HouseNumber { get; set; }
        public string PostCode { get; set; }
        public string LocationName { get; set; }
        public string Country { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string DateOfBirth { get; set; }
    }

That's the requested data in the API documentation:

{
  "shopId": "20071992",
  "orderProcessId": "ref001",
  "ipAddress": null,
  "macAddress": null,
  "customerId": "cus001",
  "billingAddress": {
    "type": "Consumer",
    "businessName": null,
    "firstName": "Martin",
    "lastName": "Früh",
    "co": null,
    "street": "Funkenbüelstrasse",
    "houseNumber": "1",
    "postCode": "9243",
    "locationName": "Jonschwil",
    "country": "CH",
    "email": null,
    "phone": null,
    "dateOfBirth": null
  },
  "shippingAddress": null,
  "orderAmount": 1200
}

When i look at the model while debugging there should be all the required data:

enter image description here

enter image description here

enter image description here

Does someone know why this happens?

If you need more code or information just say it and i edit the question.


Solution

  • I finally found the error.

    The problem was that the business name on the database was null even tho the address type was a business address.

    enter image description here

    Thanks for everyone who wrote a comment.