Whenever I try to send a post request to an api endpoint, I keep encountering an error with status code 500.
name: "HttpErrorResponse"
ok: false
status: 500
statusText: "Internal Server Error"
Below is the code I am using:
var selectedIds = ["31"];
let sendData = new FormData();
sendData.append('auth', this.dataService.REG_AUTH);
sendData.append('identifier', identifier);
sendData.append('selected[]', selectedIds);
this.http.post<any>('APIENDPOINT', sendData).subscribe(data => {
console.log(data);
}, error => {
console.log(error);
});
The problem seems to be occurring at this line:
sendData.append('selected[]', selectedIds);
. I'm unsure about how to pass an array to FormData correctly.
I have a functional example from our android app that I need to convert into Angular/Typescript syntax:
@JvmSuppressWildcards
@FormUrlEncoded
@POST("APIENDPOINT")
fun addData(
@Field("auth") auth: String,
@Field("identifier") identifier: String,
@Field("selected[]") selected: ArrayList<String>
): Call<ResponseBody>
After trying some hardcoded fixes without success, I suspect it might be an issue with serialization or data parsing in Angular:
sendData.append('selected%5B%5D', '%2231%22');
sendData.append('selected%5B%5D', '31');
sendData.append('selected%5B%5D', 31);
sendData.append('selected%5B%5D', '%5B%2231%22%5D');
sendData.append('selected%5B%5D', selectedIds);
sendData.append('selected%5B%5D', JSON.stringify(selectedIds));
Using selected
instead of selected[]
does not yield any errors, but no data is updated either. This leads me to believe it's related to serialization or parsing issues.