Easier way to transform FormData into query string
You could use URLSearchParams const queryString = new URLSearchParams(new FormData(myForm)).toString()
You could use URLSearchParams const queryString = new URLSearchParams(new FormData(myForm)).toString()
Here’s another Solution using request body: RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart(“param1”, param1) .addFormDataPart(“param2”, param2) .build(); apiInterface.somePostMethod(requestBody).enqueue( //onResponse onFailure methods ); here’s my api inteface POST method @POST(“somePostMethod”) Call<ResponseBody> somePostMethod(@Body RequestBody body); Hope it helps.
Your error InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable appears because you must call setRequestHeader after calling open. Simply move your setRequestHeader line below your open line (but before send): xmlhttp.open(“POST”, url); xmlhttp.setRequestHeader(“x-filename”, photoId); xmlhttp.send(formData);
You can use form-data – npm module. because formData() isn’t NodeJS API Use it this way, var FormData = require(‘form-data’); var fs = require(‘fs’); var form = new FormData(); form.append(‘my_field’, ‘my value’); form.append(‘my_buffer’, new Buffer(10)); form.append(‘my_file’, fs.createReadStream(‘/foo/bar.jpg’));
If your view is based on a model and you have generated the controls inside <form> tags, then you can serialize the model to FormData using var formdata = new FormData($(‘form’).get(0)); This will also include any files generated with <input type=”file” name=”myImage” …/> and post it back using $.ajax({ url: ‘@Url.Action(“YourActionName”, “YourControllerName”)’, type: ‘POST’, data: … Read more
The solution to the problem is to explicitly set Content-Type to undefined so that your browser or whatever client you’re using can set it and add that boundary value in there for you. Disappointing but true.
For Chrome, Safari and Firefox, just use this: form.append(“blob”, blob, filename); (see MDN documentation)
Update: the XHR spec now includes several more functions to inspect FormData objects. FireFox has supported the newer functions since v39.0, Chrome is slated to get support in v50. Not sure about other browsers. var form = document.querySelector(‘form’); var formData = new FormData(form); for (var [key, value] of formData.entries()) { console.log(key, value); } //or console.log(…formData)
New in Chrome 50+ and Firefox 39+ (resp. 44+): formdata.entries() (combine with Array.from() for debugability) formdata.get(key) and more very useful methods Original answer: What I usually do to ‘debug’ a FormData object, is just send it (anywhere!) and check the browser logs (eg. Chrome devtools’ Network tab). You don’t need a/the same Ajax framework. You … Read more
I’ve finally managed to do it. Answer in code snippet below: var querystring = require(‘querystring’); var request = require(‘request’); var form = { username: ‘usr’, password: ‘pwd’, opaque: ‘opaque’, logintype: ‘1’ }; var formData = querystring.stringify(form); var contentLength = formData.length; request({ headers: { ‘Content-Length’: contentLength, ‘Content-Type’: ‘application/x-www-form-urlencoded’ }, uri: ‘http://myUrl’, body: formData, method: ‘POST’ }, … Read more