POST local storage - angular

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • railcats
    New Member
    • May 2022
    • 1

    POST local storage - angular

    I'm trying to POST my local storage to an API, but everytime I do it, I get a status: 400. Title of error says '"One or more validation errors occurred."'

    I must be doing this wrong. I'm trying to loop through all the key value pairs and then send that via a POST.

    Code:
      public postInfo() {
        for (let i = 0; i < localStorage.length; i++) {
          const key = localStorage.key(i);
    
        const headerDict = {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Access-Control-Allow-Headers': 'Content-Type',
        }
    
        const requestOptions = {
          headers: new HttpHeaders(headerDict),
        };
    
          this.http.post(this.url, key, requestOptions).subscribe(data => {
            console.log(data);
            });
          }
        
      }
    Also here is how I'm saving my local storage:

    Code:
    addToCart(product: Product) {
        var sendJson = JSON.stringify(product.productID);
        var jsonData = JSON.stringify(product.productName)
        localStorage.setItem(sendJson, jsonData);
      }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    There are two things you can do: check if the response specifies the errors, and if not, look at the request (in the dev tools) and manually check whether the request conforms to the API's requirements.

    Comment

    Working...