I'm trying to send a PUT request from Angular2 (using typescript) to an API made with Laravel. I'm doing it with the FormData class because this is how I can attach a file to the request. The POST method is working great, but when I try to update my model with a PUT method the API receives an empty request.
I've made some debugging and the FormData contains the data as expected, so I'm almost sure that my data is getting lost at the PUT request.
Service:
import {Http, Headers, Response, RequestOptions} from "@angular/http";
@Injectable()
export class SensorService {
public data: Data[] = [];
public sensors: Sensor[] = [];
constructor(private http: Http,
private authService: AuthService) {
}
updateSensor(id: number, nombreNevera: string, ciudad: string, empresa_id: number,
tipoSensor: string, telefonoMarcado: number,
telefonoMarcadoB: number = null, telefonoMarcadoC: number = null,
telefonoMarcadoD: number = null, fechaCalibracion: number = null,
temMax: number = null, temMin: number = null, humeMax: number = null,
humeMin: number = null, correos: any = null, fileToUpload: any = null) {
let formData: FormData = new FormData();
formData.append('nombreNevera', nombreNevera);
formData.append('cuidad', ciudad);
formData.append('empresa_id', empresa_id);
formData.append('tipoSensor', tipoSensor);
formData.append('telefonoMarcado', telefonoMarcado);
formData.append('telefonoMarcadoB', telefonoMarcadoB);
formData.append('telefonoMarcadoC', telefonoMarcadoC);
formData.append('telefonoMarcadoD', telefonoMarcadoD);
formData.append('fechaCalibracion', fechaCalibracion);
formData.append('humeMax', humeMax);
formData.append('humeMax', humeMin);
formData.append('temMin', temMin);
formData.append('temMax', temMax);
formData.append('correos', JSON.stringify(correos));
if (fileToUpload != null) {
formData.append('certificado', fileToUpload, fileToUpload.name);
}
const headers = new Headers({
'X-Requested-With': 'XMLHttpRequest'
});
return this.http.put(APPCONSTANTS.API_ENDPOINT + 'user/sensor/edit/' + id + '?token=' + this.authService.getToken(),
formData,
{headers: headers})
.map((response: Response) => {
return {
msg: response.json().msg
};
});
}
}
Laravel returns an "Unprocessable Entity" because of the required fields in the controller.
Content-Typeheader, and that's only if Laravel can process that type of put.Acceptheader to tell the server you want a json result. We found Firefox had an issue if we didn't have that header. developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept (Though this isn't the current issue. Try what @Z.Bagley suggested for that.)