1

The API (written in C#, ASP.NET Core) receive 3 fields: title, content and an array of categories (integers):

[HttpGet]
[AllowAnonymous]
public IActionResult CreatePublication(string title, string content, IEnumerable<int> categoryIds)
{
    return Ok();
}

The problem is the API always receive zero elements in the categoryIds array.

The client in Angular4:

let ccategoryIds = new Array<number>()
ccategoryIds.push(2)
ccategoryIds.push(5)
ccategoryIds.push(7)

let requestOptions = {
  params: new HttpParams()
    .set('title', 'The title')
    .append('content', 'The content')
    .append('categoryIds', ccategoryIds.toString()),
  withCredentials: true
}

this.http
  .get('http://localhost:53203/api/publications/createPublication', requestOptions)
  .subscribe(data => {
  },
  (err: HttpErrorResponse) => {
     alert('Error')
  })

Update

If I use JSON.stringify(ccategoryIds) instead of ccategoryIds.toString(), I receive zero elements in the API too.

enter image description here

It is not a problem of the backend. In Postman it works: enter image description here enter image description here

1 Answer 1

1

Obviously I can't see how you have written the API handler, but I thin you are using .toString() where you should be using JSON.stringify():

let ccategoryIds = new Array<number>()
ccategoryIds.push(2)
ccategoryIds.push(5)
ccategoryIds.push(7)

let requestOptions = {
  params: new HttpParams()
    .set('title', 'The title')
    .append('content', 'The content')
    .append('categoryIds', JSON.stringify(ccategoryId)),
  withCredentials: true
}

this.http
  .get('http://localhost:53203/api/publications/createPublication', requestOptions)
  .subscribe(data => {
  },
  (err: HttpErrorResponse) => {
     alert('Error')
  })
Sign up to request clarification or add additional context in comments.

1 Comment

@JohnDoe this seems like a problem on the C# side. Can we see your code there?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.