Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to hit smartfile api, this api is working fine using postman, but when i add query string parameter to url it gives me error.

I need below url to be created when request send to smartfile
https://app.smartfile.com/api/2/path/info?children=true[^]

but when my code executes It appends slash at the end of info and give me error of forbidden link. this error is only coming due to url. below url is generating from code

https://app.smartfile.com/api/2/path/info/?children=true[^]

What I have tried:

string urlParameters = "?children=true";

     HttpClient client = new HttpClient();
     client.BaseAddress = new Uri("https://app.smartfile.com/api/2/path/info");
     //Authorization added somewhere in the code,
     // Add an Accept header for JSON format.
     client.DefaultRequestHeaders.Accept.Add(
     new MediaTypeWithQualityHeaderValue("application/json"));

     // List data response.
     HttpResponseMessage response = client.GetAsync(urlParameters).Result;
Posted
Updated 7-Nov-19 8:43am

1 solution

Looks like the code is seeing your urlParameters as a relative URL and appending the slash to the base URL as it is designed to.

HttpClient.BaseAddress Property (System.Net.Http) | Microsoft Docs[^]
Remarks:
When sending a HttpRequestMessage with a relative Uri, the message Uri will be added to the BaseAddress property to create an absolute Uri.
What I would do to get around this is to not set the BaseAddress at all and pass the URL directly into the GetAsynch method
C#
string urlParameters = "?children=true";
HttpClient client = new HttpClient();

// remove next line
// client.BaseAddress = new Uri("https://app.smartfile.com/api/2/path/info");

// Authorization added somewhere in the code,
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new mediaTypeWithQualityHeaderValue("application/json"));

// List data response.
HttpResponseMessage response = client.GetAsync("https://app.smartfile.com/api/2/path/info" + urlParameters).Result;
 
Share this answer
 
v2
Comments
Faseeh Haris 7-Nov-19 15:00pm    
Super fast solution maker. Thanks, Working fine now. :)
MadMyche 7-Nov-19 16:45pm    
Glad it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900