POST data in JSON format

json post request example
how to send json object in post request in javascript
how to send json data using http post request in java
jquery post json
rest api post json example
html form post json example
how to send form data in json format
post request body: json

I have some data that I need to convert to JSON format and then POST it with a JavaScript function.

<body onload="javascript:document.myform.submit()">
<form action="https://www.test.net/Services/RegistrationService.svc/InviteNewContact" method="post" name="myform">
  <input name="firstName" value="harry" />
  <input name="lastName" value="tester" />
  <input name="toEmail" value="testtest@test.com" />
</form>
</body>

This is the way the post looks now. I need it submit the values in JSON format and do the POST with JavaScript.

Not sure if you want jQuery.

var form;

form.onsubmit = function (e) {
  // stop the regular form submission
  e.preventDefault();

  // collect the form data while iterating over the inputs
  var data = {};
  for (var i = 0, ii = form.length; i < ii; ++i) {
    var input = form[i];
    if (input.name) {
      data[input.name] = input.value;
    }
  }

  // construct an HTTP request
  var xhr = new XMLHttpRequest();
  xhr.open(form.method, form.action, true);
  xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

  // send the collected data as JSON
  xhr.send(JSON.stringify(data));

  xhr.onloadend = function () {
    // done
  };
};

Making a JSON POST Request With HttpURLConnection, 2. Building a JSON POST Request with HttpURLConnection. 2.1. Create a URL Object. 2.2. Open a Connection. 2.3. Set the Request Method. 2.4. Set the Request Content-Type Header Parameter. 2.5. Set Response Format Type. 2.6. Ensure the Connection Will Be Used to Send Content. 2.7. Create the Request Body. 2.8. JSON is the most popular data format for exchanging data between a browser and a server. The JSON data format is mostly used in web services to interchange data through API. When you working with web services and APIs, sending JSON data via POST request is the most required functionality. PHP cURL makes it easy to

Here is an example using jQuery...

 <head>
   <title>Test</title>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
   <script type="text/javascript" src="http://www.json.org/json2.js"></script>
   <script type="text/javascript">
     $(function() {
       var frm = $(document.myform);
       var dat = JSON.stringify(frm.serializeArray());

       alert("I am about to POST this:\n\n" + dat);

       $.post(
         frm.attr("action"),
         dat,
         function(data) {
           alert("Response: " + data);
         }
       );
     });
   </script>
</head>

The jQuery serializeArray function creates a Javascript object with the form values. Then you can use JSON.stringify to convert that into a string, if needed. And you can remove your body onload, too.

JSON vs POST data, The posted data can either be in JSON format, or url-encoded format. Sending data in JSON format is slightly more powerful than sending traditional POST data, because JSON allows you to send nested data. JSON Data - A Name and a Value. JSON data is written as name/value pairs. A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:

Another example is available here:

Sending a JSON to server and retrieving a JSON in return, without JQuery

Which is the same as jans answer, but also checks the servers response by setting a onreadystatechange callback on the XMLHttpRequest.

JSON requests and responses, you may provide and accept entities as JSON , a simpler and more concise format. To make a request with JSON, the appropriate HTTP headers are: 1 curl -i -u application_name:application_password --data '{"value": /user. POST � JavaScript Object Notation (JSON, pronounced / ˈ dʒ eɪ s ən /; also / ˈ dʒ eɪ ˌ s ɒ n /) is an open standard file format, and data interchange format, that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value).

Using the new FormData object (and other ES6 stuff), you can do this to turn your entire form into JSON:

let data = {};
let formdata = new FormData(theform);
for (let tuple of formdata.entries()) data[tuple[0]] = tuple[1];

and then just xhr.send(JSON.stringify(data)); like in Jan's original answer.

Postman POST JSON: How to send JSON data to an API endpoint , However sometimes a Postman POST JSON request is required as your API endpoint might not Duration: 4:39 Posted: Oct 2, 2018 Hi Rahul, Thanks for this valuable information. I need to send the mail to the testers with the Jenkins URL using my notification API. I am using the same method you mentioned above, Can you tell me how to invoke the URL in JSON data in dynamic way, since the uri will change for each build.

Write many entries to a channel in JSON format with a single HTTP , Bulk-Write JSON Data. Write many entries to a channel in JSON format with a single HTTP POST. expand all in page. Description. When my request is executed, data are passed as parameters in my request payload and not as JSON data. Here is my Request Payload: refId=585&phone=0674444444 I want to send data in json format like : { "refId":"585", "phone:"0674444444" } What am I missing please ?

How to pass JSON data using HTTP Request action - How to, There are times when you may want to post JSON data to a specific URL or a web application. To achieve this, you can use the 'HTTP Request' action in your� Steps to Build a JSON POST request. Create a URL object: Let’s create a URL object.We need a target URI string that accepts the JSON data via HTTP POST method. In this example, I am using httpbin.org service to Post JSON data. httpbin.org is a web service that allows us to test the HTTP request.

How to send a JSON object to a server using Javascript , It is a lightweight data transferring format. It is very easy to JavaScript | Sending JSON data to server. </ title > xhr.open( "POST" , url, true );. Online JSON Formatter and Online JSON Validator also provides tools to convert JSON to XML, JSON to CSV, JSON Editor, JSONLint , JSON Checker and JSON Cleaner. JSON Formatter Online and JSON Validator Online work well in Windows, Mac, Linux, Chrome, Firefox, Safari, and Edge and it's free. JSON Example: Play with JSON data: Insurance Company JSON

Comments
  • What structure should the JSON data have? Just {"firstName":"harry", "lastName":"tester", "toEmail":"testtest@test.com"}?
  • Yes the data would be in the format you described! thanks for the responses!
  • I think this is a good, clean, concise example of how to get the job done in 20 lines of code, without 100K of framework.
  • @IanKuca Thanks:) I was wondering if we can send the json data without urlencode the data? I mean I want to send data like "cmd":"<img src=0 onerror=alert(1)>" not %3Cimg+src%3D0+onerror%3Dalert%281%29%3E
  • @liweijian You should go with whatever JSON.stringify returns.
  • @IanKuca It seems that the post data was encoded by html form not JSON.stringify.
  • @liweijian you'd need to urldecode the form values first if that's the case
  • Josh, the example on jQuery shows otherwise: Looks more like a standard query-string than like JSON.
  • You guys are right. I've updated the answer accordingly. Thanks!
  • This is a good example, however according the title this should be performed using javascript, not a javascript library (like jQuery in this case)
  • You are, of course, welcome to do it the hard way. Everyone else uses jQuery.
  • The question asks how to POST data using javascript, not the jquery library. This answers the wrong question.