0

I have a c# Web api, like this:

[HttpGet]
[Route("gatewayinfo/{endpointAddress}")]
public GatewayModel GetGatewayInfo(string endpointAddress)

when I call that api I need to pass in an url as address (http://example.con)

How can I do that with HttpClient? If I put url in parameter it won't work :

var client = new HttpClient();
client.SetBearerToken(token);
var result = await client.GetStringAsync(_webApiAddress + parameter);
2
  • 2
    So what's the value of _webApiAddress + parameter just before execution? Did you verify that your API returns correct data if e.g. called with http://localhost:8080/gatewayinfo/http://www.example.com/? These slashes in the "URL" could make it difficult to parse; seems like a weird format to me with the protocoll. Maybe just do a gatewayinfo/example.com without slashes in the endpointAddress? Commented Jan 15, 2016 at 0:44
  • 1
    Have you tried encoding the url before passing it as parameter? Commented Jan 15, 2016 at 0:46

1 Answer 1

3

you will need to encode the param

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(_webapiAddress);
    client.SetBearerToken(token)

    var result  = await client.GetStringAsync($"gatewayinfo/{HttpUtility.UrlEncode(parameter)}");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I actually made a change to Convert url to base64 and pass the base64 string as parameter. At web api I convert base64 back to url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.