0

I have 2 programs on my localhost, one with REST API and another program that calls the api.

I'm trying to authenticate user(https://localhost:44301/token) with below function and getting badRequest. the same is working when testing with postman. baseUrl is "https://localhost:44301/";

 static async Task<Uri> AddCartRecordAsync(CartsTable cartsTable)
    {
        string ResponseString = "";
        HttpWebResponse response = null;
        var request = (HttpWebRequest)WebRequest.Create(Utility.baseUrl + "token");
        request.Accept = "application/json";
        request.Method = "POST";

        //Get credentials from config.
        var username = "kkk@gmail.com";
        var password = "Test123!";

        Credentials cred = new Credentials()
        {
            username = username,
            password = password,
            grant_type = "password"
        };

        var myContent = JsonConvert.SerializeObject(cred);

        var data = Encoding.ASCII.GetBytes(myContent);

        request.ContentType = "application/json";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }
        try
        {
            using (response = (HttpWebResponse)request.GetResponse())//BadRequest Here
            {
                ResponseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
        }
        catch (Exception e)
        {
            string i = "df";
        }

enter image description here

Kobi
  • 117
  • 2
  • 12
  • Do you have antiforgery token validation turned on ? Maybe it requires you to sent AF token. – AchoVasilev Jan 08 '22 at 08:23
  • No, it works when i run it with Postman... Call to Token is a built-in logic in .net - i added a screenshot of Postman – Kobi Jan 08 '22 at 08:26
  • If it works fine in Postman you'll need to explain what front-end you are using and how you are calling this. – Patrick Jan 08 '22 at 09:04
  • The function in the post is the front-end – Kobi Jan 08 '22 at 09:06
  • 1
    It looks like the endpoint do not expects json object. in postman parameters are sent in other form – spzvtbg Jan 08 '22 at 09:11
  • also you can see how looks the request in bunch of languages from postman by visiting > btn top right somewhere – spzvtbg Jan 08 '22 at 09:19
  • looks like you are right, any idea how can i change the content to urlencoded? – Kobi Jan 08 '22 at 09:35
  • im not familiar with the webrequest object but the data looks like query string , see the question hier: https://stackoverflow.com/questions/3089177/how-to-escape-url-encoded-data-in-post-with-httpwebrequest – spzvtbg Jan 08 '22 at 09:42

1 Answers1

0

The common issues are :

1-Params Body Type

2-Authentication Type : if u have bearer type you should include bearer keyword before your token in the header before sending request like "token":"bearer AuthToken"

3-method type : get put post delete

kamyar
  • 13
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 03:59