Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a RAW data POST-request with an HTML Form?

Tags:

html

post

forms

I need to send raw data in the body of a POST request to a Webservice. Can I accomplish this with a HTML form?

Using a standard HTML input field seems to unavoidably generate a POST body of the form <name_of_input_field>=<DATA> whereas I would simply like to post <DATA>.

Do I need to resort to performing this request with javascript?

like image 870
Joel Westberg Avatar asked Jul 12 '11 14:07

Joel Westberg


People also ask

How do I send a post request in HTML?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.

How can I send form data in POST request?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.


2 Answers

Can I accomplish this with a HTML form?

No.

A form can send either application/x-www-form-urlencoded or multipart/form-data data.

If you want to use a different data format you have to start looking at JavaScript and XHR (and be subject to the Same Origin Policy and so on).

You would be better writing your server side code to accept one of the above encodings instead — there are no shortage of libraries that can decode them.

like image 178
Quentin Avatar answered Sep 17 '22 22:09

Quentin


There is a potential hack here. If your data either has a natrual "=" in it, or you can modify it so that an "=" can be added in some harmless way, you can:

  • Set the forms enctype to text/plain
  • Set a hidden input w/ a name taken from the start of your your data, up to but excluding the first "="
  • Set the input fields value to the byte that follows the "=" until the end of the data.

Source where I learned about this technique:

https://systemoverlord.com/2016/08/24/posting-json-with-an-html-form.html

like image 35
covener Avatar answered Sep 20 '22 22:09

covener