C
C#10mo ago
Isaaaak

API request, working in Postman but not C#

I'm trying to call a certain API and then get a response back. When I run the code in Postman it works fine, I get the right response. But when I run it in C#, I get 403 forbidden. If I run the code in Postman without the JSON body, then I get 400 bad request. So I know it's not the JSON that's the issue, meaning it's probably the headers. Could someone give me a hint at what I should look at to solve this problem?
string jsonString = "{\"purpose\":\"MAX\",\"applicants\":[{\"birthYear\":2003,\"qualifiesEmploymentLength\":true,\"monthlyIncome\":55000,\"employmentType\":\"PERMANENT\",\"employmentRole\":\"\",\"maritalStatus\":\"SINGLE\"}],\"existingProperties\":[],\"household\":{\"children\":[],\"amortizationStudentDebt\":729,\"otherAssets\":0,\"carCost\":[{\"type\":\"OWNED\"}]},\"intermediaryType\":\"454\",\"newProperty\":{\"propertyType\":\"HOUSE\",\"mortgage\":null,\"downPayment\":700000,\"municipality\":\"Örebro\",\"livingSpace\":130}}";

var client = new HttpClient();
client.BaseAddress = new Uri("https://api.sbab.se/api/pre-approval/2.0/");

var content = new StringContent(jsonString, Encoding.UTF8, "application/json");

var response = client.PostAsync("loan-offer", content).Result;


Console.WriteLine(jsonString);
string jsonString = "{\"purpose\":\"MAX\",\"applicants\":[{\"birthYear\":2003,\"qualifiesEmploymentLength\":true,\"monthlyIncome\":55000,\"employmentType\":\"PERMANENT\",\"employmentRole\":\"\",\"maritalStatus\":\"SINGLE\"}],\"existingProperties\":[],\"household\":{\"children\":[],\"amortizationStudentDebt\":729,\"otherAssets\":0,\"carCost\":[{\"type\":\"OWNED\"}]},\"intermediaryType\":\"454\",\"newProperty\":{\"propertyType\":\"HOUSE\",\"mortgage\":null,\"downPayment\":700000,\"municipality\":\"Örebro\",\"livingSpace\":130}}";

var client = new HttpClient();
client.BaseAddress = new Uri("https://api.sbab.se/api/pre-approval/2.0/");

var content = new StringContent(jsonString, Encoding.UTF8, "application/json");

var response = client.PostAsync("loan-offer", content).Result;


Console.WriteLine(jsonString);
No description
No description
11 Replies
Jimmacle
Jimmacle10mo ago
403 suggests an authentication issue are you using the same credentials for both requests?
Isaaaak
IsaaaakOP10mo ago
there are no credentials/no auth it's not required)
Isaaaak
IsaaaakOP10mo ago
No description
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Isaaaak
IsaaaakOP10mo ago
I do think I have that, I'm not really familiar with the concept however. I just removed this serialization for debugging purposes and put the actual JSON instead
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Isaaaak
IsaaaakOP10mo ago
Jafar Jabbarzadeh
TutorialsEU
How to call POST API in C# | TutorialsEU
In this article, we will create a simple project demonstrating how to call Post API in C# and how to serialize and deserialize C# objects to and from JSON.
Isaaaak
IsaaaakOP10mo ago
but then I have to deal with tasks and stuff :(
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Isaaaak
IsaaaakOP10mo ago
anyways if we disregard this because I'm pretty sure the JSON is not the problem the problem is the headers I think Any1 got any idea?
Jimmacle
Jimmacle10mo ago
start by making your postman and C# requests match exactly, then remove things until you find what triggers the issue
Want results from more Discord servers?
Add your server