0

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.

  • Can you translate the error message ? Also did you check the API documentation to see if the payload and head values you are passing are correct? – Chetan Mar 01 '22 at 08:30
  • I just edited the question and posted the additional information. The only required data in the head is the API key as far as i know and was told. @Chetan – SomeDumbQuestions Mar 01 '22 at 08:38
  • It's impossible to answer this - if you make a bad request, you'll get a 400. We have no idea what that service requires, you didn't post your code or the full exception contents so we can't even guess what may be wrong. Perhaps the service requires some missing headers. Copy the full exception details by clicking on `Details Kopieren` and post them *as text* in the question. Some services return a standard error response in case of validation errors – Panagiotis Kanavos Mar 01 '22 at 08:40
  • Why are you using HttpWebRequest anyway? In .NET Core (which includes .NET 5 and 6) that's just a wrapper over HttpClient – Panagiotis Kanavos Mar 01 '22 at 08:41
  • @PanagiotisKanavos I added the information to the header and posted the code i had in the screenshot as normal code so it's a bit more readable. Is it understandable now? We're still using asp.net mvc 4.61 so i used httpWebRequest. – SomeDumbQuestions Mar 01 '22 at 08:47

1 Answers1

0

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.