Click here to Skip to main content
14,736,706 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Getting "the remote server returned an error (400) bad request." while getting the response from an API call(URL is provided by Client which i pass correctly).
While calling the same from postman its working properly, but from C# code it is not working.
I am getting error on below line
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream())


What I have tried:

WebRequest webRequest = WebRequest.Create(oAuthType.Auth_URL);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";

string strBLine = @"grant_type:password
                            username:***
                            password:**e
                            scope:esbcom-sms
                            client_id:ro.****e
                            client_secret:ro.**e
                            ";

char[] delims = new[] { '\r', '\n' };

string[] strings = strBLine.Replace(" ","").Split(delims, StringSplitOptions.RemoveEmptyEntries);

byte[] byteArray = Encoding.UTF8.GetBytes(strings.ToString());

webRequest.ContentLength = byteArray.Length;

StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream());
                        
requestWriter2.Write(strBLine.Replace(" ", "").Split(delims, StringSplitOptions.RemoveEmptyEntries));
requestWriter2.Close();

string responseData = string.Empty;
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
    responseData = responseReader.ReadToEnd();
}
Posted
Updated 26-Nov-20 3:18am
v2

1 solution

Quote:
byte[] byteArray = Encoding.UTF8.GetBytes(strings.ToString());
The strings variable is an array of strings.

Calling .ToString() on that variable will return the type name: "System.String[]"

Similarly, passing a string array to StreamWriter.Write will call .ToString() on that array, and send the literal string "System.String[]" to the request.

Hence your request is invalid.

Even if you sent the individual lines correctly, it would not be a correct application/x-www-form-urlencoded payload.

Fix your code to send the correct data in the request:
WebRequest webRequest = WebRequest.Create(oAuthType.Auth_URL);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";

const string requestBody = "grant_type=password&username=***&password:**e&scope:esbcom-sms&client_id:ro.****e&client_secret:ro.**e";

webRequest.ContentLength = Encoding.UTF8.GetByteCount(requestBody);

using (StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream()))
{
    requestWriter.Write(requestBody);
}

string responseData;
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
    responseData = responseReader.ReadToEnd();
}

If it still doesn't work, then you need to capture your network request and compare it to the request sent by Postman.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900