0

There are many questions about this already on this forum. I have read them but i couldn't solve my issue, so I'll try and ask again in here.

I'm building an Angular + ASP Core app. I have used FromBody succesfully on other requests already, but for some reason this particular one isn't working.

Here's my model class:

export interface CreateEventModel {

    name: string;
    description: string;
    categoryId: number;
    typeId: number;
    price: number;
    startDate: Date;
    endDate: Date;

    imageContent: Blob;
    imageMimeType: string;
}

Here's how i'm sending to server:

 plan() {

   // ...

    this.net.post('Event/Create', this.model).subscribe((res: Response | any) => {

        if (res.status == 200 || res.isOk == true) {
            this.toasterService.pop('success', 'Sucess', "IS NOT NULL!");
        }
    }, err => {  // Show error  });

}

Heres my '.net.post' Service in the Request, in case you're wondering:

post<T>(url: string, data: any): Observable<T> {
    return this.basePut<T>(url, data);
}

// Does header, with JWT
private basePut<T>(url: string, data: any): Observable<T> {
    let options = this.doHeaders();
    return this.doSub(this.http.post(this.doUrl(url), data, options));
}


private doSub<T>(obs: Observable<Response>) {
    var ob1: Observable<T> = obs.map(this.extractData)
        .catch(this.handleError);

    return ob1;
}

Heres my JSON output:

{
    "imageContent": {},
    "imageMimeType": "image/jpeg",
    "name": "MyTitle",
    "typeId": 3,
    "categoryId": 3,
    "description": "My Description",
    "price": 0,
    "startDate": "2018-08-10T15:55:00.000Z",
    "endDate": "2018-09-26T15:55:00.000Z"
}

Here's my controller method:

[HttpPost, Authorize(Roles = "Admin,Planner")]
public IActionResult Create([FromBody]CreateEventModel model)
{
    // Do stuff, but 'model' is always null
}

Lastly, my C# Model

 public class CreateEventModel: ICreateEventModel
{

    public string Name { get; set; }
    public string Description { get; set; }
    public int CategoryId { get; set; }
    public int TypeId { get; set; }
    public decimal? Price { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

    public byte[] ImageContent { get; set; }
    public string ImageMimeType { get; set; }
}

I'm not sure what could be wrong. Any help would be greatly appreciated.

Lucas Arruda
  • 533
  • 1
  • 8
  • 15
  • Have you tried removing `FromBody` in the parameter definition? WebApi by default will use the body contents for any POST request so it shouldn't be needed. Also, ensure the request includes the `Content-Type: application/json` HTTP header. It's likely the Angular library does that for you but you should verify it. – Sixto Saez Feb 09 '18 at 16:27
  • 1
    My guess is that the imageContent is causing your problem. What happens if you remove that property from the JSON object being sent to the server from the client? – peinearydevelopment Feb 09 '18 at 16:27
  • @SixtoSaez i did try to remove, but it didnt work. The request includes application/json header. – Lucas Arruda Feb 09 '18 at 16:33
  • @peinearydevelopment Pricesly, imageContent is causing my problem :@ Thanks very much! Now i'll try and figure out how to properly pass an Image from Angular to Core. – Lucas Arruda Feb 09 '18 at 16:43
  • 1
    You can look here as well: https://www.strathweb.com/2018/02/exploring-the-apicontrollerattribute-and-its-features-for-asp-net-core-mvc-2-1/#toc_4 NOTE: this is for asp.net core 2.1, but he has a link there for previous versions as well. – peinearydevelopment Feb 09 '18 at 16:45
  • @peinearydevelopment Thank you very much, i’ll def look into it! – Lucas Arruda Feb 09 '18 at 16:47
  • In Chrome devtools you can capture the raw HTTP request.if you post it here, you can share it with us – zaitsman Feb 10 '18 at 11:24

0 Answers0