Getting JSON response with 400 bad request in Angular 2

handle 400 bad request in angular 6
httperrorresponse angular 6
httperrorresponse (headers: httpheaders, status: 200, statustext: ok)
angular get error message from response
angular 2 http get example
400 (bad request) angular 4 post
angular https request
angular http error handling

Im trying to call REST API of Magento 2 from Angular 2.

Facing this issue from very long and really need a fix on the same or atleast suggestion on where the issue is?

Below is how im calling REST from Angualar:

@Injectable()
export class ProductService {
public _productUrl = 'http://10..../Mage_ang2/index.php/rest/V1/customers/1';

constructor(private _http: Http) { }

getProducts(): Observable<IProduct[]> {
let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
headers.append('Authorization', 'Bearer ntthnrbj1uam2tuv1ekva7n8jh18mcnkby3');
let options = new RequestOptions( {method: RequestMethod.Get, headers: headers });


console.log(headers);
    return this._http.get(this._productUrl,options)
        .map((response: Response) => <IProduct[]> response.json())
        .do(data => console.log('All: ' +  JSON.stringify(data)))
        .catch(this.handleError);
}

When i post the same in Postman, i get a response.

When i run through Angular2, with same headers, I get a JSON response but with 400 (Bad Request) !

Also the response doesnt come to actual request call.

I have been using same syntax in my sample application but without authorization header. Everything seems correct.

The issue may with setting authorization header. Check the authorization token for correctness.

Try this syntax for setting headers

let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8';Authorization: **token** });

Getting JSON response with 400 bad request in Angular 2, When i run through Angular2, with same headers, I get a JSON response but with 400 (Bad Request) ! Also the response doesnt come to actual request call. Home » Android » android – Retrofit 2.0 – How to get response body for 400 Bad Request error? android – Retrofit 2.0 – How to get response body for 400 Bad Request error? Posted by: admin May 10, 2020 Leave a comment

Try this, it should work

 let headers = new Headers({
        'Content-Type': 'application/json',
        'Authorization':'Bearer ntthnrbj1uam2tuv1ekva7n8jh18mcnkby3'
    });

 return this._http.get(url,{headers:headers})
        .map(response: any => {
            return response.json();
        });

Handle 400 Bad Request From WebApi In Angular 6 · Issue #26817 , Below is an Asp.net Core WebAPI which returns bad request with Error details The response is being handled at Angular side as follows: user.service.ts above method being. When I tried to do Postman I get perfect result as below (with error description): +1 3 -1 2. @aledpardo. Copy link. Quote reply  Solved: Hi, I am trying to update a version in a project using the REST api via .NET I can sucessfully do GETs but PUTs and POSTs fail with a '400

try

const headers = new Headers({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ntthnrbj1uam2tuv1ekva7n8jh18mcnkby3' });    

let options = new RequestOptions({ headers: headers });

return this._http.get(this._productUrl,options)

HttpErrorResponse, A response that represents an error or failure, either from a non-successful HTTP status, an error while executing the request, or some other failure which  An in-depth explanation of what a 400 Bad Request Error response code is, including tips to help you resolve this error in your own application.

Handling Errors In Angular - Part Ten, In this article you will learn about Handling Errors In Angular. When it comes to handling errors, we have 2 types of errors. Whenever we want to get the object from the server or update or delete it is Bad Request (400) the server may respond with an error message which we call the bad request. The first post I’ll share is a scenario of a problem I was having with communicating with a RESTful WCF service. For some reason all I was getting back from the service was an HTTP 400 – Bad Request response when attempting to make a POST to a url. So, here’s what I was attempting to do. I created a simple WCF RESTful web service.

Introduction to error handling in Angular 7 - Part 2, This is a deep dive into HTTP error handling in Angular 7, you might These codes are usually three digit codes like 400 which, signifies the request sent was unexpected This is a demo jsonplaceholder application where parts of the to actually get response from the server, you can resend the request  Receive 400 "Bad request" when attempting to Post message via C# app and Json Chad Yarber Nov 22, 2015 I am trying to post to a message string using json to our HipChat rooms.

ASP.NET Core and Angular 2, externalProviderLogin(" + JsonConvert. all the method into a try/catch block, so any given Exception would result in a 400 - Bad Request HTTP error response. I don't actually get a REST response. I create my request, send it for a response and .NET throws a System.Net.WebException that simply states "The remote server returned an error: (400) Bad Request." Has anyone used REST to Jira from .NET before? Is there something I am doing wrong? Thanks

Comments
  • what is the console in browser says??
  • I have attached the screenshot for the browser console, which says 400 but i get a json data
  • Have updated the error which im getting in broswer console @Aravind...Have done enough search on this refetch and CORS issue, Need help on identifying where the issue is?
  • Any suggestions @aravind?
  • nope. not enough information. seems like server side issue.
  • Could you please tell what is the change in ur code and mine!! and where are you using get ()??
  • @Sach right, there is get(). You can see the diff by comparing the request Headers. I have not tried
  • @Sach Can you compare the request header of postman and angular?
  • Exactly the same headers, using the same content type and Auth Bearer key values..!!
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.