'HttpClient.post is sucessful but not sending data to json

I am trying to store data from my angular app into a JSON. The JSON is stored within the assets folder of my app and I have the app on a server.

I am using HttpClient to POST data to the JSON. It says it is successful but it does not actually send the data.

This is my .ts:

import { HttpClient } from '@angular/common/http'
import { HttpHeaders } from '@angular/common/http'

export class OpscreenComponent implements OnInit {

  constructor(private httpService: HttpClient ) { }

  ngOnInit() {
    var jsonpost = {
      "testing": [
        {
          "anothertest":"here",
          "anumber": 1
        }
      ]
    }
    var headers = new HttpHeaders({
      'Content-Type': 'application/json'
    })
    var options = { headers: headers }
    this.httpService.post("http://servername/angularApp/assets/testing.json", jsonpost, options)
    .subscribe(
      data=> {
        console.log("POST Request is Successful ", data )
      },
      error => {
        console.log("Error ", error)
      }
    )

  }

}

I get no error messages and the request is successful because it is logging POST Request is Successful null in the console.

The json is blank to start and after the POST success it is still blank (hence the null in the console.)

Why is it not posting the data to the JSON?

I am using httpClient for get so I know it is imported correctly.



Solution 1:[1]

this.httpService.request('POST', `${YourTestUri}`, { body: jsonpost, headers: headers }).pipe( switchMap( (response: any) => ... ) );

Solution 2:[2]

What you are trying to do is write files to disk, unfortunately, The HttpClient of Angular can't write files to disk. At least not with the classic implementation.

You probably prefer to add a server who can write to the disk, and your HttpClient will hit the server with the corresponding data.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Eliran Eliassy