2

I am trying to consume REST API from a xamarin forms app using the following code. however for some strange reason I am getting Invalid URI Error. I also tried using an Absolute Path in the PostAsync Method but still the error persists. Can someone guide me on this please?

HttpClient client = new HttpClient(); 
string baseAdd = @"http://localhost:9000/";

    public async void GenerateAPIToken()
    {

        string tsResult = "";
        try
        {

            Token token = new Token();
            //GET TOKEN
            client.BaseAddress = new Uri(baseAdd);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpRequestMessage msg = new HttpRequestMessage();
            msg.Content = new StringContent(@"{""username"":""admin"",""password"":""admin123""}");


            HttpResponseMessage response = await client.PostAsync(client.BaseAddress+"token/generate.php", msg.Content);


            if (response.StatusCode == HttpStatusCode.OK)
            {
                HttpContent cnt = response.Content;
                tsResult = await cnt.ReadAsStringAsync();
                token = JsonConvert.DeserializeObject<Token>(tsResult);
                App.apiToken = token.Document.AccessToken;
            }

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

    }
Jawad
  • 11,028
  • 3
  • 24
  • 37
cvdev
  • 21
  • 1
  • 3
  • string baseAdd has the following value: http://localhost:9000/ please see code above since in the oomment it is dropping down slahses and Http protocol – cvdev Aug 02 '20 at 11:51
  • `client.BaseAddress+"token/generate.php"` - - the BaseAddress is already being added by HttpClient. This means that it will be added twice. You also probably want your BaseAddress to start with `http://` or `https://` – canton7 Aug 02 '20 at 11:55
  • @canton7 I have tried passing only"token/generate.php" and also provide an absolute URL in the PostAsync parameter but still the same error. the base address includes protocal http:// it is just being dropped from the commend on stackoverflow for some reason – cvdev Aug 02 '20 at 11:58
  • Does this answer your question? [Why is HttpClient BaseAddress not working?](https://stackoverflow.com/questions/23438416/why-is-httpclient-baseaddress-not-working) – Jawad Aug 02 '20 at 12:41

1 Answers1

2

It looks like you're setting the BaseAddress, and sending an absolute URI. When the BaseAddress is set, it is being added as a prefix to the URI. You must either set the BaseAddress and send a relative URI, or send an absolute URI without setting the base address. see https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.baseaddress?view=netcore-3.1

Uri Y
  • 840
  • 5
  • 13
  • @cvdev Either don't set the client.BaseAddress, or don't concatenate it in the client.PostAsync. – Uri Y Aug 02 '20 at 12:04
  • I have tried to provide the whole URL in the PostAsyc method but still the same error strangely enough – cvdev Aug 02 '20 at 12:10
  • Tried also but still same error pops up....cant understand why have been working with REST API for a very long time – cvdev Aug 02 '20 at 12:35