Symfony 4 & Postman access File from post-data POST request - symfony

I'm trying to do an image upload using a Symfony REST API.
However, I'm having problems trying to read the POST request file parameter on Symfony's side using Postman.
On Postman I have the following body on the POST request:
form-data POST request with key=file and value=some image
When the Request reaches Symfony, I have the following:
public function uploadImage(Request $request)
{
$params = $request->request;
$file = $params->get('image');
dump($file);
...
}
The file variable is null. I have also tried using $params = $request->files but the result is the same.
If I dump $request->getContent() I can see the file is passing through, but cannot have access to it to do what I need.
Am I missing something?

I managed to figure it out, it has something to do with the Postman app I'm using.
Basically, when I choose form-data on the body, the Headers tab is not automatically updated to something like Content-Type => multipart/form-data, it just keeps the previous one it existed (application/json, for example).
When I found this out, I changed the Content-Type to multipart/form-data but it was still not working.
It only worked when I unticked the Content-Type on Headers.
Why is that?
I was missing the boundary part on the Content-Type, which Postman automatically generates when the body is a form-data and no Content-Type header is generated.
So,
Content-Type = multipart/form-data -> doesn't work
Content-Type = multipart/form-data; boundary=----WebKitFormBoundary5Qa5cbeHtIOCMAKa -> works fine (you don't need to specify this, as Postman will make a request with this set)

I think what you should use is $request->files->get('file');

Related

Change the response status code for ngx.exec in Openresty

I am returning a stored custom HTML file for a particular request using ngx.exec function. I want to set the right HTTP response code. I am not able to figure out how to do that. Even if I set ngx.status before ngx.exec the response code returned is always 200 to the client.
Is there a way to set the response code?

Is there a way to set the http Header values for an esp_https_ota call?

I'm trying to download a firmware.bin file that is produced in a private Github repository. I have the code that is finding the right asset url to download the file and per Github instructions the accept header needs to be set to accept: application/octet-stream in order to get the binary file. I'm only getting JSON in response. If I run the same request through postman I'm getting a binary file as the body. I've tried downloading it using HTTPClient and I get the same JSON request. It seems the headers aren't being set as requested to tell Github to send the binary content as I'm just getting JSON. As for the ArduinoOTA abstraction, I can't see how to even try to set headers and in digging into the esp_https_ota functions and http_client functions there doesn't appear to be a way to set headers for any of these higher level abstractions because the http_config object has no place for headers as far as I can tell. I might file a feature request to allow for this, but am new to this programming area and want to check to see if I'm missing something first.
Code returns JSON, not binary. URL is github rest api url to the asset (works in postman)
HTTPClient http2;
http2.setAuthorization(githubname,githubpass);
http2.addHeader("Authorization","token MYTOKEN");
http2.addHeader("accept","application/octet-stream");
http2.begin( firmwareURL, GHAPI_CERT); //Specify the URL and certificate
With the ESP IDF HTTP client you can add headers to an initialized HTTP client using function esp_http_client_set_header().
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_set_header(client, "HeaderKey", "HeaderValue");
err = esp_http_client_perform(client);
If using the HTTPS OTA API, you can register for a callback which gives you a handle to the underlying HTTP client. You can then do the exact same as in above example.

Use script to form request body in IntelliJ integrated http client

In pseudo-whish-language this is what I try to achieve in IntelliJ integrated http client:
POST {{basepath}}/upload
Content-Type: application/json
{
"content": "{% btoa(await fetch('my-file.dat')) %}"
}
Normally, one would use the following to upload a file raw or as part of a multipart request:
< my-file.dat
But in my case, the binary file has to be encapsulated in a json and encoded using base64. I tried putting the file into a variable, but I can't find a way to run a script (for setting the variable) before the request is sent and also it doesn't seem possible to use script directly as part of the request. And I'm also not sure if I'm able to access external files from the script.
I cannot send it as multipart as proposed in Add file to multipart form request in IntelliJ HTTP Client because my server doesn't accept non-json requests.
Do I have any other options or is this a missing feature of this http client?
I did some tricks to achieve something similar.
I created an Environment Variable with the data parsed to base64 format.
{
"dev": {
"png_image_base64": "--your format data in bae64--"
}
}
Then, I added this as a reference in my HTTP request.
### ADD FILE TO TRACKING
POST http://localhost/files
Content-Type: application/json
Authorization: Bearer {{personal_access_token}}
{
"file": "{{png_image_base64}}"
}
Finally, run the request with the dev environment.
You could create many vars with different files of content: pdf_base64, png_bas464, etc. depending on your necessities. The "bad thing" is that you need to parse manually each of these files, but only once, after that, you could use it on a json and perform many tests.
I hope that it could be useful for someone.
Regards.

Checking a JSON POST API with Pingdom

Is there any way to add a check in Pingdom that makes an HTTP POST request to an API with a JSON POST body? I see in their advanced configuration screen that you can specify a POST body but it indicates that it sends the request as a form post, not JSON.
I tried specifying the JSON in the POST body section and setting the "Content-Type" header manually to "application/json", but that doesn't appear to work.
We just set the Content-Type header to application/json and it worked nicely.

Error with 'Access-Control-Allow-Origin'

I have a webpage I made with angular 2 that is a form and at the end of it the form must be sent to my java server. But I'm unable to send, I get the error Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
I have already changed my server so it will send the Access-Control-Allow headers. The function is like this:
public static Response buildResponse(int status, Object reponseObject, MediaType mediaType) {
Response.ResponseBuilder rb = Response.status(status).entity(reponseObject);
if (mediaType != null) {
rb = rb.type(mediaType);
}
rb = rb.header("Access-Control-Allow-Origin", "*");
rb = rb.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
rb = rb.header("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token, accept");
rb = rb.header("Access-Control-Allow-Credentials", true);
System.out.println("teste");
return rb.build();
}
I have also tried to add the header 'Access-Control-Request-Method': 'POST' to the page request, but I still get the error. The problem is not with the software functionality itself because it works fine if I install and activate the CORS plugin for chrome, but I can't request the user to install the plugin to use my site. Does anyone knows what I'm missing? I have been sarching for sometime and the only solutions I found was to disable this browser security (I can't ask the user to do that) and to add the headers on the request, what I already have.
I was finally able to solve my problem. The problem was in the communication between my page and my server. The CORS block is imposed by the browser and the server needs to send a header authorizing the access (the headers I put in the function in the question). What I was missing was the header in the OPTION method. My JavaScript code automatically send and OPTIONS request (the preflight request) before the actual GET or POST method. What I did was to manually implement the OPTIONS method in the server that respond with the necessary headers.

Resources