Click here to Skip to main content
15,887,386 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem in POSTMAN parameters
I want to get a token from URL.
in postman parameters Body set to

Raw

{
    "UserName": "xxx",
    "Password": "xxx"
}


application/json; charset=utf-8

and Method is POST

In PostMAn it returns token ; but in c# by Httpclient I could not find how can do it

Thanks a lot

What I have tried:

var client = new HttpClient();

            client.BaseAddress = new Uri("xxxx");


            client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

            var url = "xxxx";

            var logonOptions = new
            {
                username = "xxx",
                password = "xxx",
            };

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var requestBodyAsString = serializer.Serialize(logonOptions);
            StringContent content =
                new StringContent(
                    requestBodyAsString,
                    Encoding.UTF8,
                    "application/raw"
                );

            HttpResponseMessage response;
            string contributors="";
            try
            {
                response = await client.PostAsync(url, content).ConfigureAwait(false);
                response.EnsureSuccessStatusCode();
                var resp = await response.Content.ReadAsStringAsync();
                contributors = JsonConvert.DeserializeObject<string>(resp);
                
            }
            catch(Exception ex)
            {

            }
Posted
Updated 20-Apr-21 22:03pm

1 solution

Quote:
in postman parameters Body set to
...
application/json; charset=utf-8

...
C#
new StringContent(... "application/raw");
Your C# code is sending a content type of application/raw, which doesn't match the content type sent in your Postman request.

The API you're calling doesn't understand how to process an application/raw body, so it will most likely return a "bad request" status.

Which you would know if you weren't swallowing all exceptions and throwing away the error details!
 
Share this answer
 

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