Use json objects. Do something like that. Don't write json by hand, you could easily do a mistake.

public JSON() throws JSONException
{
    JSONArray locArr=new JSONArray();
    locArr.put(createLatLng(40.900799, 8.606102));
    locArr.put(createLatLng(42.900799, 9.606102));
    JSONObject main=new JSONObject();
    main.put("locations", locArr);
    Log.d("JSON",main.toString());      
}


public JSONObject createLatLng(double lat, double lng) throws JSONException
{
    JSONObject latLng=new JSONObject();
    latLng.put("lat",lat);
    latLng.put("lon",lng);
    JSONObject latLngWrap=new JSONObject();
    latLngWrap.put("latLng",latLng);
    return latLngWrap;
}
Answer from maciekczwa on Stack Overflow
🌐
Atlassian
developer.atlassian.com › server › crowd › json-requests-and-responses
JSON requests and responses
These samples show the JSON representations that the Crowd REST Resources expect to receive. ... { "name" : "my_username", "first-name" : "My", "last-name" : "Username", "display-name" : "My Username", "email" : "user@example.test", "password" : { "value" : "my_password" }, "active" : true }
Discussions

How to pass values (parameters) into a JSON request body for a POST API | OutSystems
How to pass values (parameters) into a JSON request body for a POST API More on outsystems.com
🌐 outsystems.com
php - POST request with JSON body - Stack Overflow
I would like to add a post to a Blogger blog via PHP. Google provided the example below. How to use that with PHP? You can add a post for a blog by sending a POST request to the post collection URI with a post JSON body: More on stackoverflow.com
🌐 stackoverflow.com
c# - Passing JSON as a string in the body of the POST request - Stack Overflow
I need a small help, because I don't know how to solve the below problem. The requirement is simple, I have to sent the JSON to the server as a string parameter. The server basing on the key finds ... More on stackoverflow.com
🌐 stackoverflow.com
Adding Json Body to a HTTP request
Hi Need to add the below request to a HTTP Request. However I cant workout how to escape the double quotes, I’ve tried using “”" but it doesn’t seem to be accepted by the editor Code I want { “type”: “select”, “whe… More on forum.uipath.com
🌐 forum.uipath.com
18
2
March 23, 2021
🌐
Baeldung
baeldung.com › home › web › sending json http request body in terminal
Sending JSON HTTP Request Body in Terminal | Baeldung on Linux
March 18, 2024 - In this tutorial, we’ll be learning how to send JSON objects as the request body correctly with the help of the Content-Type HTTP header. In Linux, curl and wget are the common terminal-based HTTP clients. Given its popularity in the Linux ecosystem, we’ll be using them in this article for demonstration purposes. To obtain curl and wget, we can install them using the package manager. For example...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Request › json
Request: json() method - Web APIs | MDN
November 7, 2025 - The request body cannot be parsed as JSON. ... const obj = { hello: "world" }; const request = new Request("/myEndpoint", { method: "POST", body: JSON.stringify(obj), }); request.json().then((data) => { // do something with the data sent in the request });
🌐
ReqBin
reqbin.com › req › 4rwevrqh › post-json-example
How do I post JSON to the server?
January 16, 2023 - The Content-Type request header specifies the media type for the resource in the body. Additionally, you can pass an "Accept: application/json" header, which tells the server that the client is expecting JSON data. In this POST JSON example, we send JSON data to the ReqBin echo URL with the ...
Find elsewhere
🌐
Practical Go Lessons
practical-go-lessons.com › post › go-how-to-send-post-http-requests-with-a-json-body-cbhvuqa220ds70kp2lkg
Go: How to send POST HTTP requests with a JSON body| Practical Go Lessons
Suppose you want to run this request: POST https://example.com/teacher. With the following JSON body: { "id": "42", "firstname": "John", "lastname": "Doe" } The first step is to build the body.
🌐
Medium
medium.com › @guptadiksha88 › best-practices-for-handling-json-request-bodies-in-post-api-calls-1b19fc6a8da7
“Best Practices for Handling JSON Request Bodies in POST API Calls” | by Diksha Gupta | Medium
May 24, 2023 - Common content types include “application/x-www-form-urlencoded” for form data and “application/json” for JSON data. The server, upon receiving the POST request, processes the data. Once the server has processed the request, it sends an HTTP response back to the client, indicating the status of the operation. Now let’s discuss one POST request as shown below ... Here, through this request, we want to create a new placeholder resource on the server by providing all the required values in the request body.On the server side, the new resource will be created with all the provided information and a response will be sent back to the client.
🌐
Swagger
swagger.io › docs › specification › v3_0 › describing-request-body › describing-request-body
Describing Request Body | Swagger Docs
The requestBody is more flexible in that it lets you consume different media types, such as JSON, XML, form data, plain text, and others, and use different schemas for different media types. requestBody consists of the content object, an optional Markdown-formatted description, and an optional required flag (false by default). content lists the media types consumed by the operation (such as application/json) and specifies the schema for each media type. Request bodies are optional by default. To mark the body as required, use required: true. ... content allows wildcard media types. For example, image/* represents all image types; */* represents all types and is functionally equivalent to application/octet-stream.
🌐
FastAPI
fastapi.tiangolo.com › tutorial › body
Request Body - FastAPI
For example, this model above declares a JSON "object" (or Python dict) like:
🌐
Baeldung
baeldung.com › home › http client-side › making a json post request with httpurlconnection
Making a JSON POST Request With HttpURLConnection | Baeldung
May 11, 2024 - In this article, we learned how to make a POST request with JSON content body using HttpURLConnection.
Top answer
1 of 5
153

You need to use the cURL library to send this request.

<?php
// Your ID and token
$blogID = '8070105920543249955';
$authToken = 'OAuth 2.0 token here';

// The data to send to the API
$postData = array(
    'kind' => 'blogger#post',
    'blog' => array('id' => $blogID),
    'title' => 'A new post',
    'content' => 'With <b>exciting</b> content...'
);

// Setup cURL
$ch = curl_init('https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts/');
curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array(
        'Authorization: '.$authToken,
        'Content-Type: application/json'
    ),
    CURLOPT_POSTFIELDS => json_encode($postData)
));

// Send the request
$response = curl_exec($ch);

// Check for errors
if($response === FALSE){
    die(curl_error($ch));
}

// Decode the response
$responseData = json_decode($response, TRUE);

// Close the cURL handler
curl_close($ch);

// Print the date from the response
echo $responseData['published'];

If, for some reason, you can't/don't want to use cURL, you can do this:

<?php
// Your ID and token
$blogID = '8070105920543249955';
$authToken = 'OAuth 2.0 token here';

// The data to send to the API
$postData = array(
    'kind' => 'blogger#post',
    'blog' => array('id' => $blogID),
    'title' => 'A new post',
    'content' => 'With <b>exciting</b> content...'
);

// Create the context for the request
$context = stream_context_create(array(
    'http' => array(
        // http://www.php.net/manual/en/context.http.php
        'method' => 'POST',
        'header' => "Authorization: {$authToken}\r\n".
            "Content-Type: application/json\r\n",
        'content' => json_encode($postData)
    )
));

// Send the request
$response = file_get_contents('https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts/', FALSE, $context);

// Check for errors
if($response === FALSE){
    die('Error');
}

// Decode the response
$responseData = json_decode($response, TRUE);

// Print the date from the response
echo $responseData['published'];
2 of 5
13

I think cURL would be a good solution. This is not tested, but you can try something like this:

$body = '{
  "kind": "blogger#post",
  "blog": {
    "id": "8070105920543249955"
  },
  "title": "A new post",
  "content": "With <b>exciting</b> content..."
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/blogger/v3/blogs/8070105920543249955/posts/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: OAuth 2.0 token here"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($ch);
Top answer
1 of 1
4

The error clearly suggests that your JSON is not correct. If we analyze the payload property:

{
  "key": "test",
  "payload": "[{"IDNew":1,"NameNew":"t1","DescriptionNew":"t1d", "IntegerValueNew":1, "DecimalValueNew":123.3}]"
}

It seems you are creating a string object which further contains a JSON as a string. Generally, when you pass an array, you would pass it like this.

{
  "key": "test",
  "payload": [
    {
      "IDNew": 1,
      "NameNew": "t1",
      "DescriptionNew": "t1d",
      "IntegerValueNew": 1,
      "DecimalValueNew": 123.3
    }
  ]
}

But, since value of the payload property is not properly escaped, which is why it is not properly able to parse it as it has unexpected characters for a string value.

If you strictly want to pass a JSON Array as a string object, you need to properly escape it in order to get it working. For example below is a JSON that contains JSON as a string with properly escaped properties:

{
  "key": "test",
  "payload": "[{\"IDNew\":1,\"NameNew\":\"t1\",\"DescriptionNew\":\"t1d\", \"IntegerValueNew\":1, \"DecimalValueNew\":123.3}]"
}

This is how you would escape your JSON if you strictly want to pass a JSON object that further contains JSON as string.

Or, perhaps, use single quote (') instead for the nested JSON. For example below is a JSON that contains JSON as a string with a single quotes for the properties:

{
  "key": "test",
  "payload": "[{'IDNew':1,'NameNew':'t1','DescriptionNew':'t1d', 'IntegerValueNew':1, 'DecimalValueNew':123.3}]"
}

UPDATE

I just wanted to add a suggestion that would be less confusing and would generate an accurate output for the scenario.

It would be nice if you generate the models for your intended JSON string and serialize the model to get a JSON string then do the assignment to payload property.

var payload = new List<payloadSample1>();
payload.Add(new payloadSample1{ IDNew = 1, NameNew = "t1", DescriptionNew = "t1d" });
var payloadStr = JsonConvert.SerializeObject(payload);
// payloadStr would contain your JSON as a string.

In C#, you can also generate dynamic type objects. Use those if your JSON is constantly varying and you find it hectic to create many models for many api requests.

var payload = new List<dynamic>();
payload.Add(new { IDNew = 1, NameNew = "t1", DescriptionNew = "t1d" });
var payloadStr = JsonConvert.SerializeObject(payload);
// And even then, if you have a further JSON object to send:
var payloadParent = new { key = "test", payload = payloadStr };
// send payloadParent as your json.

This is not the cleanest approach because of many reasons one out of those would be, when there is a change in your model, you will have to manually analyze all your dynamic objects and change all the references where you are using it. But, certainly, it will reduce the confusion behind escaping and maintaining the strings.

If you are using JavaScript make the api call, then generate a proper JSON object and then stringify it to get a string.

🌐
Retool
community.retool.com › 💬 queries and resources
API REST POST Request body in JSON is treated as string - 💬 Queries and Resources - Retool Forum
March 8, 2024 - I've been trying to setup a POST request in my REST API; however I'm having an issue with the body I send on the request; the body contains a nested object, but the payload is always sent as a string; I've followed the s…
🌐
Reddit
reddit.com › r/htmx › how to send a json in the request body using htmx ?
r/htmx on Reddit: How to send a Json in the request body using HTMX ?
January 29, 2025 -

Hi all, I am trying to create a button that will send a request to server with a Json in the body. But i keep getting the json as FORM in the request while the body is empty. I read that we need json-enc extension for sending the json is body so i did use that still the same issue. What am i doing wrong. Here is the button code.

<button hx-post="/userManagement/updateUserState"
        hx-ext="json-enc"
        hx-vals='{"users": ["{{.Email}}", "testing@yoyo.com"]}'
        class="button is-danger is-rounded">Disable</button>

{"users":"testing@yoyo.com","csrf":"AtouXBVbEupRVSzHwhUXrzzSwlJoLhNv"}

This is the request payload when sent. and i get bad request error. please point out what i am doing wrong thanks.

🌐
Apidog
apidog.com › articles › send-json-object-with-post-request
How to Send JSON Object with POST Request
April 30, 2024 - Subsequently, initiate a "New Request," input the server endpoint address in the request URL field, and select the request type as POST. Navigate to the "Body" tab, choose "json." Notably, you do not need to set the Content-Type header to application/json in Apidog; simply input your JSON data in the provided text box.
🌐
ReqBin
reqbin.com › req › v0crmky0 › rest-api-post-example
How do I post JSON to a REST API endpoint?
In this REST API POST request example, the server informs the REST API client that it has returned JSON by sending Content-Type: application/json header in response. Server response to our test REST API POST request. ... The official MIME type for JSON is application/json. To POST JSON data to the server, you must specify the data type in the body of the POST message using the appropriate Content-Type request header.