SOLVED C# HttpWebResponse delivers different response than Postman

1.1k Views Asked by At

I'm consuming a Web API of an internal system in the company. It's getting a payload in JSON format and returning a response with data in JSON format. When sending the request with Postman, it returns the expected response (StatusCode=200 + a response text in JSON format). That means that everything is OK with the web service.

Now I have to develop an application in C# to send this HTTP request. The problem is, that I receive as response content "OK" and not the expected JSON response gotten with Postman.

    public HttpWebResponse SendRequest(string url, string checkOutFolder, string drawingNo, string login, string password)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "application/json";
        request.Method = "GET";
        request.Accept = "application/json";

        string payload = GeneratePayLoad(checkOutFolder, drawingNo);
        string header = CreateAuthorization(login, password);

        request.Headers[HttpRequestHeader.Authorization] = header;
        request.ServicePoint.Expect100Continue = false;

        var type = request.GetType();
        var currentMethod = type.GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(request);

        var methodType = currentMethod.GetType();
        methodType.GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentMethod, false);

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            streamWriter.Write(payload);
        }

        // Response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader rd = new StreamReader(response.GetResponseStream()))
        {
            string responseContent = rd.ReadToEnd();
            Console.WriteLine(responseContent);
        }
        return response;
    }

Has anyone already experiences something similar. Can you help me?

EDIT Following your suggestions 1) Changed the method to POST -> result is still the same 2) Used Postman's code generator and RestSharp -> result is still the same

        public void Request(string url, string checkOutFolder, string drawingNo, string login, string password)
    {
        var client = new RestClient(url);
        var request = new RestRequest();
        request.Method = Method.Post;
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Authorization", "Basic **********");
        var body = GeneratePayLoad(checkOutFolder, drawingNo);
        request.AddParameter("application/json", body, ParameterType.RequestBody);
        var response = client.Execute(request);
        Console.WriteLine(response.Content);
    }
  1. Changed to HttpClient -> result still the same

             using (var client = new HttpClient())
         {
             string uri = "******************";
             string path = "destinationpath";
             var endpoint = new Uri(uri);
             string payload = GeneratePayLoad(path, "100-0000947591");
             //FormUrlEncodedContent form = new FormUrlEncodedContent(payload);
             var stringContent = new StringContent(payload);
             var payload2 = new StringContent(payload, Encoding.UTF8, "application/json");
             var byteArray = Encoding.ASCII.GetBytes("*******");
             client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
             var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(client.DefaultRequestHeaders.Authorization.ToString()));
             var result = client.PostAsync(endpoint, stringContent).Result.Content.ReadAsStringAsync().Result;
             Console.WriteLine("test");
    
         }
    
    1. Wrote a Python code using requests Package -> delivers the same as Postman. So the problem is in the C# code.

Does anyone have an idea what is going on?

SOLVED The issue was on the payload generation!

1

There are 1 best solutions below

4
Lzh On

The request needs to be an HTTP POST and not HTTP GET because it contains JSON payload.

request.Method = "GET"; should be request.Method = "POST";

That would be one of the issue(s). I am not sure if there is something else that is wrong, but try changing the request method to POST and try again.