1

I tried to connect a REST API using the postman and it's always a good request. No problems.

But, in the rest implementation code I always receive the error "StatusCode: Unauthorized, Content-Type: text/plain; charset=utf-8, Content-Length: 0)".

I've tried many ways to do this but it never done.

//url = url server
//authorization = Bearer .....
//body = text json 

var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("authorization", authorization);
request.AddParameter("application/json", body, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

var result = response.Content;

In the postman

enter image description here

enter image description here

The server doesn't receive the Authorization token when I try to do it in the code.

demo
  • 6,038
  • 19
  • 75
  • 149
  • 1
    The server can't tell whether you're using Postman or C# or any other thing to post to it. So if you compare the HTTP requests, spot the difference and that will probably be what's causing their server to treat the requests coming from your code differently than what's coming from Postman. Pay particular attention to the request headers, such as the user agent. – mason Apr 22 '19 at 14:18
  • 1
    Use a sniffer like wrieshark or fiddler and compare the http headers in postman with c# code that doesn't work. Solution is to make c# http headers look exactly like postman. – jdweng Apr 22 '19 at 14:26
  • can you please tell us how you are creating an authorization in code. – Hitesh Anshani Apr 22 '19 at 14:28
  • The server don't receive the token when I try to do it in the code. – Elias Alves Apr 22 '19 at 14:30
  • I will try to do it. @jdweng – Elias Alves Apr 22 '19 at 14:33
  • This may be of use: https://stackoverflow.com/questions/36061745/converting-httpclient-to-restsharp – aleppke Apr 22 '19 at 17:25
  • It's working now, thank you guys. I will post the answer. – Elias Alves Apr 23 '19 at 11:20

1 Answers1

1

I am using the HttpWebRequest but I think it's also possible using the RestClient.

I used the Fiddler to identify the headers in the postman request and then I reply this headers in the code.

The code below is working to me.

I will make some changes but that's it.


//url = url server
//authorization = Bearer .....
//body = text json 
//bytesBody = body in byte[]

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.PreAuthenticate = true;
webRequest.Method = "POST";

webRequest.Headers["Cache-Control"] = "no-cache";
webRequest.Accept = "*/*";
webRequest.Headers["Accept-Encoding"] = "gzip, deflate, br";

webRequest.Headers["Accept-Language"] = "en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36";
webRequest.ContentType = "application/json";

webRequest.ContentLength = bytesBody.Length;
webRequest.Headers["authorization"] = authorization;
//webRequest.Headers["Origin"] = "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop";

webRequest.KeepAlive = true;
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Host = host;


using (Stream dataStream = webRequest.GetRequestStream())
{
    dataStream.Write(bytesBody, 0, bytesBody.Length);
    dataStream.Flush();
    dataStream.Close();
}

WebResponse response = webRequest.GetResponse();

using (var streamReader = new StreamReader(response.GetResponseStream()))
{
    string result = streamReader.ReadToEnd();
}
response.Close();