0

In my Angular 7 project I'm attempting to upload an image file along with other form data to an Apache PHP backend. Using the HttpClient to make the request, POSTing just an image by itself works fine.

// This works fine
uploadAvatar(payload: any): Observable<any> {
    const formData = new FormData();
    formData.set('avatar', payload.avatar);
    return this.http.post(`${api_url}/user/avatar`, formData)
}

// This does not
create(payload: any): Observable<any>{
    const formData = new FormData();
    formData.set('title', payload.title);
    formData.set('image', payload.image);
    return this.http.post(`${api_url}/path/to/add`, formData);
}

The POST request is never made due to it failing the OPTIONS preflight, in which the server complains that the image file is missing from the request data.

Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I have tried settings headers manually to Content-Type: multipart/form-data and Content-Type: application/x-www-form-urlencoded and neither have worked.

I have tried using UrlSearchParams and HttpParams instead of FormData, both of which also gave me no luck.

When the request is made in Postman, it works fine.

2 Answers 2

0

When you do the request from Postman, CORS is not involved - the browser implements the CORS protocol, Postman does not.

So, I'm guessing that there's something wrong with the way the PHP application is set up for handling that particular URL - it is apparently not responding properly to the OPTIONS request that the browser sends for the pre-flight check (and which Postman does not send).

You're not showing the full error message, but it appears that the PHP app is responding to the OPTIONS request with a 404 or some other non-200 series response code.

Sign up to request clarification or add additional context in comments.

2 Comments

The backend shouldn't need to explicitly have logic for handling OPTIONS on that URL though should it? When echoing the body sent, it appears empty. But again that is for the options request.
It depends on how your framework (or non-use of framework) responds - I'm not familiar with PHP, but some back-end frameworks in other languages require that you specify both the path and HTTP method for a request handler. If you only specify POST for a particular path, and don't specify OPTIONS, you may produce a 404 for the OPTIONS request.
0

1) I agree with initial answer that it sounds like a CORS issue with the '/path/to/add' endpoint, so i would definitely check the backend first to see whats the difference between the 2 endpoints wrt CORS implementation. As part of CORS debugging, I'd be looking in your browser console's network tab at the failed requests / response to see what headers are being sent from the client. It's possible your project is already adding HttpHeaders (e.g. some projects implement a HttpInterceptor to add HttpHeaders to all requests), and it's important to know what actually is being sent for a given request.

2) I'm curious as to why you added "in which the server complains that the image file is missing from the request data."..did you see a specific error on client or server-side logs? Sharing that might suggest an additional cause.

3) A similar post here suggests trying without 'multi-part/form-data' headers..worth a shot: Upload image with HttpClient

3 Comments

There are no other CORS issues present and I have a service method that uploads an image by itself as shown in the example and that works just fine. The error that is returned from the server states that the file has not been included even though it's being sent the exact same way as the other image with the additional of other form data. To clarify, uploading an image by itself works, but with additional formdata, does not.
Hard to help you debug when we're only seeing part of the code..not sure if you tried recommendation #3 above either which appears to describe the same symptom you reported. Some related articles: developer.mozilla.org/en-US/docs/Web/API/FormData/set stackoverflow.com/questions/48468957/…
Thanks for your help. After being able to work with the backend today it turns out that the issue was in fact server side and very bizarre.

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.