🌐
ReqBin
reqbin.com › req › 4rwevrqh › post-json-example
How do I post JSON to the server?
January 16, 2023 - 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 ...
🌐
DEV Community
dev.to › ldakanno › making-a-post-request-using-json-server-h7c
Making a POST request using json-server - DEV Community
December 11, 2022 - To make any request other than GET, you will have to specify the method in which you are trying to use, in this example it would be POST since we are making a POST request. The header will communicate what kind of data we will be sending. The JSON.stringify method is really neat.
🌐
ReqBin
reqbin.com › req › v0crmky0 › rest-api-post-example
How do I post JSON to a REST API endpoint?
To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You must also specify the data type using the Content-Type: application/json request header.
🌐
JSON Editor Online
jsoneditoronline.org › home › data-fetching › post-json
How to POST JSON data in JavaScript | Indepth | JSON Editor Online
February 8, 2023 - When exchanging data, the body often is JSON data. But it can be anything, for example an HTML file, a JPG image, or a PDF document. Here is an example of a typical GET request, meant to fetch data. Normally, the request does not contain a body, and the response will contain the requested data. In this case we request information about a product, a phone, with id 2. Here an example of a typical POST ...
🌐
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 - To send a POST request, we’ll have to set the request method property to POST: ... Set the “content-type” request header to “application/json” to send the request content in JSON form.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-post-json-data-to-server
How to Post JSON Data to Server ? - GeeksforGeeks
August 1, 2024 - A send button for sending the input data to the server. When the user hits the send button it makes a POST request with JSON data to the /data route on the server, and then the server logs the received data. Example: In this example, we will make use of fetch API to send data to the NodeJS server...
🌐
DEV Community
dev.to › serenepine › how-to-send-json-data-in-postman-90a
How to Send JSON Data in Postman - DEV Community
October 20, 2024 - In the request settings, you will find a dropdown menu. Select this menu and set the request type to "POST." This is because we will be sending JSON-formatted data to the server, and POST requests are commonly used for such data transmission.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1180617 › send-a-request-to-a-restapi-with-json
Send a request to a restapi with json - Microsoft Q&A
Here in this post, I'll share one of my step-by-step examples which uses an online REST API service (https://northwind.vercel.app) which allows interaction with Northwind API. This example uses HttpClient and JsonConvert to get or post data:
🌐
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 }
Find elsewhere
🌐
Apidog
apidog.com › articles › send-json-object-with-post-request
How to Send JSON Object with POST Request
April 30, 2024 - In API testing and development, sending POST requests with JSON data is a fundamental skill. A detailed guide is provided for crafting POST requests with JSON payloads within the user-friendly interfaces of both Postman and Apidog.
🌐
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.
🌐
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 });
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);
🌐
Apidog
apidog.com › articles › postman-send-json
How to Send JSON Data in Postman
April 23, 2024 - So, how do you send JSON using Postman? The following section provides a detailed Postman POST request body JSON example.
Top answer
1 of 3
2

You don't need to build up your JSON using JObject. You can either use anonymous classes or paste your JSON sample using Paste JSON as Classes. Based on your JSON sample an anonymous object would look like this.

var body = new
{
    accountrefrence = "XXXXX",
    messages = new[]
    {
        new
        {
            to = "XXXX",
            body = "XXX!"
        }
    }
}

And actual classes might look like this:

public class Rootobject
{
    public string accountreference { get; set; }
    public Message[] messages { get; set; }
}

public class Message
{
    public string to { get; set; }
    public string body { get; set; }
}
2 of 3
0

The easiest way to manage JSON serialization is to use objects instead of raw strings or trying to manually compose the output (as it looks like you're doing here).

As you're already using Newtonsoft libraries for it, it will be quite easy to do.

Frist thing would be to create an object that represents the data you want to send to the api. As stated in another answer here, you can simply do this by copying your sample JSON and in VS do a "Paste JSON as classes".

Most likely, the resulting classes will be something like:

public class Rootobject
{
    public string accountreference { get; set; }
    public Message[] messages { get; set; }
}

public class Message
{
    public string to { get; set; }
    public string body { get; set; }
}

What you can do now is a method that grabs your data and populates the properties of this objects. As you're not providing much details on what are you doing I will simply assume that it is possible for you to have a method that receives the string values somehow.

    public void ComposeAndSendJson(string accountReference, string toAddress, string messageBody)
    {
        RootObject whatIwanttoSend = new RootObject();
        Message messageComposed = new Message();

        messageComposed.to = toAddress;
        messageComposed.body = messageBody;

        whatIwanttoSend.accountReference = accountReference;

        //I'm doing a pretty bad aproach but it's just to ilustrate the concept
        whatIwanttoSend.messages.toList().Add(messageComposed);

        var jsonData = JsonConvert.SerializeObject(whatIwanttoSend);

        //As you're working on async, you may need to do some working on here. In this sample i'm just calling it in Sync. 
        var ApiResponse = PostAsync("YOURENDPOINT",jsonData).Result();

        //Do something else with the response ... 

    }


    protected async Task<Task<HttpResponseMessage> PostAsync(Uri endpoint, object payload)
    {
        using (var httpClient = NewHttpClient())
        {
            //You have to tell the API you're sending JSON data
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Execute your call
            var result = await httpClient.PostAsJsonAsync(endpoint, payload);

            //Basic control to check all went good.
            if (result.IsSuccessStatusCode)
            {
                return result;
            }

            //Do some management in case of unexpected response status here... 
            return result; //Statuscode is 400 here. 
        }
    }

I hope this sets you in the right path.

🌐
CodeSignal
codesignal.com › learn › courses › basics-of-http-requests-with-javascript › lessons › sending-data-with-post-requests-using-javascript
Sending Data with POST Requests Using JavaScript
Let's walk through an example of how to craft a POST request to add a new to-do item to our API. First, we will need to prepare the data we wish to send. Here, we'll be adding a new to-do item with a specific title, a completion status, and a description. The data is structured as a JavaScript object: In JavaScript, you can use the Fetch API to send the request body by utilizing the fetch() method with async/await. Before sending the data, it needs to be converted into a JSON string using the JSON.stringify() method.
🌐
NetBurner
netburner.com › NBDocs › Developer › html › example_j_s_o_n-_simple_json_post_receiver.html
NetBurner 3.5.7: Simple JSON Post Receiver Example
A NetBurner NNDK application that demonstrates how to receive JSON objects via HTTP POST requests from external RESTful APIs or JavaScript within web pages.
🌐
Go language Tutorial
golangtutorial.dev › home › tips › how to make http post json request in go
How to Make HTTP POST JSON request in Go - Go language Tutorial
January 23, 2021 - I am using a third party REST API url https://reqres.in/api/users to fake the the HTTP POST request. package main import ( "bytes" "fmt" "io/ioutil" "net/http" ) func main() { httpposturl := "https://reqres.in/api/users" fmt.Println("HTTP JSON POST URL:", httpposturl) var jsonData = []byte(`{ "name": "morpheus", "job": "leader" }`) request, error := http.NewRequest("POST", httpposturl, bytes.NewBuffer(jsonData)) request.Header.Set("Content-Type", "application/json; charset=UTF-8") client := &http.Client{} response, error := client.Do(request) if error != nil { panic(error) } defer response.Body.Close() fmt.Println("response Status:", response.Status) fmt.Println("response Headers:", response.Header) body, _ := ioutil.ReadAll(response.Body) fmt.Println("response Body:", string(body)) }
🌐
DummyJSON
dummyjson.com › docs › posts
Posts - DummyJSON - Free Fake REST API for Placeholder JSON Data
fetch('https://dummyjson.com/posts/add', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'I am in love with someone.', userId: 5, /* other post data */ }) }) .then(res => res.json()) ...