-1

I'm trying to call a rest-api in asp.net to test if people are trustworthy with paying by invoice. The problem is that i've never used any rest-api before and couldn't find a good example on how to call it in asp.net. Can someone help me?

Here are the values that the request needs.

enter image description here

/api/v1/RiskCube/claim

{
  "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
}
8
  • Are you trying to call a dotnet api or you are calling another API from dotnet code?
    – Serhii
    Feb 10, 2022 at 11:27
  • I'm trying to call another API from dotnet code Feb 10, 2022 at 11:28
  • Nice. Do you have any question? Are you facing any problem?
    – ˈvɔlə
    Feb 10, 2022 at 11:29
  • Yes sadly i can't find any reference on how to call it exactly. Feb 10, 2022 at 11:30
  • @TimSpilka Take a look at System.Net.HttpClient
    – ˈvɔlə
    Feb 10, 2022 at 11:33

2 Answers 2

0

So, you can try out using HttpClient to call another API. Ex. https://learn.microsoft.com/ru-ru/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0 . And you can use libraries for that like Refit https://github.com/reactiveui/refit

edit: Link in english https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0

0
-1

Thanks for all the answers in the end i came out with that result.

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://service-zs.riskcube.ch/api/v1/RiskCube/claim");
        request.ContentType = "application/json";
        request.Method = "POST";
        request.Headers.Add("Authorization", "X-API-KEY"); //Add a valid API Key
        if (creditReformModel != null)
        {
            string postData = JsonConvert.SerializeObject(creditReformModel);
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(postData);
            }
        }
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

I think for most people @Serhii answer is better than what i came up with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.