Request a Web Service with only token

Class library usage, coding and language questions.
PWOIng
Posts: 2
Joined: Thu Mar 22, 2018 3:29 pm

Request a Web Service with only token

Post by PWOIng » Thu Mar 22, 2018 3:34 pm

Hi,

I am new on ranorex, and I try to request an API web service, I tried to add a web reference, but I have no login or password, only a permanent token, how can I do that?

(Ranorex 8.0.1)

User avatar
Vega
Posts: 109
Joined: Mon Jul 11, 2016 2:15 pm

Re: Request a Web Service with only token

Post by Vega » Fri Mar 23, 2018 9:22 pm

Its hard to say without knowing more information about your example. the Web Reference functionality in Ranorex Studio is part of.NET framework and is not Ranorex specific as far as I know; have you been able to achieve what you are describing in another .NET IDE like Visual Studio?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Request a Web Service with only token

Post by krstcs » Fri Mar 23, 2018 9:31 pm

Our API uses the OAuth token system, basically a custom header value that has to be added to the header of each request.

Code: Select all

public static HttpResponseMessage Post(string uri, string oAuthToken, string postJson) {
  using (HttpClient client = new HttpClient())
  {
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", oAuthToken);  // <-- Notice this line
    
    HttpContent content = new StringContent(postJson, System.Text.Encoding.UTF8, "application/json");
    
    return client.PostAsync(uri, content).Result;
  }
}
Shortcuts usually aren't...

PWOIng
Posts: 2
Joined: Thu Mar 22, 2018 3:29 pm

Re: Request a Web Service with only token

Post by PWOIng » Mon Mar 26, 2018 8:36 am

Thanks guys, I did it, it was very helpfull :)