Link to home
Start Free TrialLog in
Avatar of Gani tpt
Gani tpt

asked on

How do we receive list of documents from Web service using Web API..?

How do we receive list of docs number while calling Web API client.

I am getting error : "Status Code: 406, ReasonPhrase:Not Acceptable',....

But, the service is correct and all other remaining values are correct. but, finally we are getting the error.

Scrrenshot attached for reference (Status Code).

Source is below:
------------------------

 	    
	    private const string URL = "http://testURL.net/getDocs";
            private const string DATA = @"{""EmpId"": ""101"",""EmpName"": ""ANDREWS"",""Dept"": ""Accounts""}";
	    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            client.BaseAddress = new System.Uri(URL);
            byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

            System.Net.Http.HttpContent content = new StringContent(DATA, UTF8Encoding.UTF8, "application/x-www-form-urlencoded");
            HttpResponseMessage messge = client.PostAsync(URL, content).Result;
            string description = string.Empty;
            if (messge.IsSuccessStatusCode)
            {
                string result = messge.Content.ReadAsStringAsync().Result;
                description = result;
            }

Open in new window


What is the problem in the above code..? How do we receive the list of doc numbers..?
StatusCode.png
Avatar of Gani tpt
Gani tpt

ASKER

any update...
Avatar of David Johnson, CD
What is the service API need for input? and what does it return? check the service documentation
Input :  {""EmpId"": ""101"",""EmpName"": ""ANDREWS"",""Dept"": ""Accounts""}

Method : post

Receiving output (List<string>) :  { "ACToo1","IMN201","RAK101",....}... ====> Doc numbers as output...

we tried to execute in third party tool. (SoapUI 5.4.0).. it is working fine.. below is the output..

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Authorization, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, x-csrf-token
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 28 Nov 2018 07:40:44 GMT

["ACToo1","IMN201","RAK101"]

But, we are not sure where we have to change in our code...?
Isn't your request payload JSON data? But in the headers you have marked it as "application/x-www-form-urlencoded"

Try using "application/json" content type.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thnaks to all for great suggestions..