Click here to Skip to main content
14,383,854 members
Rate this:
Please Sign up or sign in to vote.
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 9:43am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
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;
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100