0

I am working on an angular app which was created from the dotnet template. I am getting a 400 bad request when I attempt to post to my controller. I have three other methods on the controller, all Get, and they work completely fine.

Controller:

        [HttpPost("[action]")]
        public async Task UpdateVehicle([FromBody]string content)
        {
            using(var client = new DocumentClient(
                new Uri(config["cosmos:endpoint"]), config["cosmos:passkey"]))
            {
                await client.ReplaceDocumentAsync(JsonConvert.DeserializeObject<Document>(content));
            }
        }

Button event: (disabled set to false when an edit made)

    <div id="json-editor" class="col-md-7">
      <button id=submitjson #savebutton [disabled]=true (click)=submitJson()>Save</button>
      <json-editor [options]="options" [data]="currentFile"></json-editor>
    </div>

Component:

    @ViewChild('savebutton', { static: false }) saveButton: ElementRef;
    @ViewChild(JsonEditorComponent, { static: false }) editor: JsonEditorComponent;

    ...

    this.options.onChange = () => this.saveButton.nativeElement.disabled = false;
    ...

    submitJson(){
          var altered = JSON.stringify(this.editor.get());
    
          const httpOptions = {
            headers: new HttpHeaders({
              'Content-Type':  'application/json'
            })
          };
    
          this.http.post<any>(this.baseUrl + 'Ingestion/UpdateVehicle', this.editor.get(), httpOptions).subscribe(
            {error: error => console.error('there was an error', error)}
          );
        }

I have tried the enableCors route and it did not work. When I debug through I find the error:

error:
errors:
$: Array(1)
0: "The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
length: 1
__proto__: Array(0)
__proto__: Object
status: 400
title: "One or more validation errors occurred."
traceId: "|cfc492aa-4e4427da20e8bfb6."
type: "https://tools.ietf.org/html/rfc7231#section-6.5.1"

I am thinking this is when the .net core controller is trying to cast my json value into the string argument. As some of you may tell I attempted to stringify it first but that has not had any affect.

I came across a post yesterday about a replacement MS have made to remove NewtonSoft and it is causing this issue. It is not related to the DeserialzeObject() call as I never get that far. I cannot find a reference to it this morning and not sure how to fix this. Is this the issue? Does anyone know how to fix this?

onesixtyfourth
  • 744
  • 9
  • 30

1 Answers1

0

Found the answer here but would appreciate an explanation if anyone knows it. Changed the controller to:

        [HttpPost("[action]")]
        public async Task UpdateVehicle([FromBody]Object content)
        {
            using(var client = new DocumentClient(
                new Uri(config["cosmos:endpoint"]), config["cosmos:passkey"]))
            {
                await client.ReplaceDocumentAsync(JsonConvert.DeserializeObject<Document>(content.ToString()));
            }
        }   
onesixtyfourth
  • 744
  • 9
  • 30