2
Answers

API show 400 Bad Request

Photo of Mohsin Arif

Mohsin Arif

5y
728
1
hello guys 
is there any issue in this code , because i m getting 400 bad request error,
  1. public async Task<ActionResult> Index()  
  2.       {  
  3.           List<ModelHome> EmpInfo = new List<ModelHome>();  
  4.   
  5.           using (var client = new HttpClient())  
  6.           {  
  7.               //Passing service base url  
  8.               client.BaseAddress = new Uri(Baseurl);  
  9.   
  10.               client.DefaultRequestHeaders.Clear();  
  11.               //Define request data format  
  12.               client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  13.               
  14.               client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);  
  15.               client.DefaultRequestHeaders.Add("Access-Key", AccessKey);  
  16.                 
  17.   
  18.               //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
  19.               HttpResponseMessage Res = await client.GetAsync("api/Fee/GetDetail?challan_number=4780415&branchCode=24&organizationUserId=445");  
  20.                 
  21.               //Checking the response is successful or not which is sent using HttpClient  
  22.               if (Res.IsSuccessStatusCode)  
  23.               {  
  24.                   //Storing the response details recieved from web api   
  25.                   var EmpResponse = Res.Content.ReadAsStringAsync().Result;  
  26.   
  27.                   //Deserializing the response recieved from web api and storing into the Employee list  
  28.                   EmpInfo = JsonConvert.DeserializeObject<List<ModelHome>>(EmpResponse);  
  29.   
  30.               }  
  31.               //returning the employee list to view  
  32.               return View(EmpInfo);  
  33.           }  
  34.       }  
 

Answers (2)

0
Photo of Salman Beg
156 12.1k 624.2k 5y
400 error means the request which you are trying to access is either incorrect or corrupted by the server. This is a problem with the API. So check API first. Thanks.
0
Photo of Rajanikant Hawaldar
32 38.8k 456.6k 5y
This might help you https://stackoverflow.com/questions/19671317/400-bad-request-http-error-code-meaning https://airbrake.io/blog/http-errors/400-bad-request https://kinsta.com/knowledgebase/400-bad-request/ https://www.howtogeek.com/359267/what-is-a-400-bad-request-error-and-how-can-i-fix-it/amp/