Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# RestSharp prevent request redirect on 302

Tags:

rest

c#

restsharp

I'm trying to implement a third party service on my project and i need to do some authentication to consume their API.

To do so i need to execute a POST request on their auth url, the problem is when i execute the request their server sends a response with a 302 and the Location header, now the weird part:

I need to get this location link and read the query string to get the information i need.

I have already tried everything i could think of, even Postman wont work with it.

This is my code:

var client = new RestClient(APIBase + "/portal/rest/oauth2/login");

var request = new RestRequest(Method.POST);

request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("cache-control", "no-cache");
request.AddParameter("application/x-www-form-urlencoded", $"user={User}&password={Password}&client_id={ClientId}", ParameterType.RequestBody);

var content = client.Execute(request);

This is the response headers i managed to get using curl on the command prompt:

HTTP/1.1 302 Moved Temporarily
Date: Wed, 26 Jul 2017 17:59:32 GMT
Server: Apache-Coyote/1.1
Location: LOCATION-LINK
Content-Length: 0

HTTP/1.1 200 OK
Date: Wed, 26 Jul 2017 17:59:32 GMT
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"20095-1497998126000"
Last-Modified: Tue, 20 Jun 2017 22:35:26 GMT
Content-Type: text/html
Content-Length: 20095
Vary: Accept-Encoding

Is it possible ?

like image 389
Ariel Avatar asked Jul 26 '17 18:07

Ariel


1 Answers

You can disable RestSharp redirects with:

client.FollowRedirects = false;

And then just check that response status code is 302 and read Location header.

like image 138
Vitaliy Avatar answered Oct 05 '22 22:10

Vitaliy