Sending JSON data through an HTTP POST request using Arduino

I am attempting to send JSON data using an Arduino. When running this code, I attempt to send the JSON data with a QueryString parameter. However, when I try this code, the server responds with a message stating that the QueryString format is incorrect, indicating that I am successfully connected to the server and it has received my data.

 if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("POST /URL?query=jsondata HTTP/1.1");
client.println("Host: **.**.**.**");
client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);

}

However, my primary objective is to send the JSON data with a QueryString parameter. When I try the following code;

client.println("POST /URL?query={request:{Header:{Username:kullaniciAdi,Password:123456},Item:{Serial:ABC123QWE,Data:100, DateOn:23/11/1986 15:45:24}}} HTTP/1.1");

I receive a HTTP Error 400, indicating that the request is malformed. Does anyone have any ideas on how to resolve this issue?

Answer №1

A void, your URL has spaces and possibly other characters that can throw off the structure of a post request. It's important to encode these characters.

From what I've discovered, it seems that the standard Arduino libraries don't have a built-in urlEncode function like many other programming languages and libraries do. You'll either need to create one yourself or seek out an existing solution.

Here's an example of how your code might look:

String request = "/URL?query={request:{Header:{Username:kullaniciAdi,Password:123456},Item:{Serial:ABC123QWE,Data:100, DateOn:23/11/1986 15:45:24}}}";
String encRequest = uriEncode(request); // you'll need to implement your own encoding method for this...
String post = "POST " + encRequest + " HTTP/1.1");
client.println( post);

You can find some helpful discussions on creating a uriEncode function on the Arduino Forum, and there's also a useful method outlined on hardwarefun.com.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered while making an http get request for a node that returns JSON

I've been struggling with this issue for quite some time now. While I've come across similar problems on Stack Overflow, none of the suggested solutions seem to work for me. I keep encountering the following error message: undefined:1 SyntaxErro ...

What is the best method for retrieving a URL from a property within a JSON object?

Looking to access the video URLs from Frontendmasters courses through an API endpoint? The JSON object returned by the endpoint includes the URLs in the 'lessondata' property under 'sourcebase'. Wondering how to extract these URLs and s ...

Encountered a problem while trying to retrieve JSON data from Cassandra DB using Java and sparkSession

I am currently working on a project that involves reading data from a Cassandra table using Java with sparkSession. The goal is to format the output as JSON. Here is the structure of my database: CREATE TABLE user ( user_id uuid, email ...

Loop through each element in a JSON array using jQuery's `.each()` method

Above is a snippet of my jQuery function: function fetchData(jsonLink){ $(".scrollable .items").html("<img class='loadGif' src='/sites/all/themes/zen/journeyon/images/ajax-loader.gif' alt='' />"); $.ajaxSetup({ ...

Access specific data within a JSON array in PHP without the need for a foreach loop

Lately, I've been facing some challenges when it comes to decoding and interpreting the prices for specific items in the bitskins api. For instance, the OPSKINS API provides a straightforward output: {"status":1,"time":1477116462,"response":{"AK-47 ...

The specified message formats required for the operation include 'XML' and 'JSON'

Creating a WCF REST service includes defining methods for adding, deleting, and editing news entities. Here is an example of such a service: [ServiceContract] public interface INewsRepository { [OperationContract] [WebInvoke(Me ...

Utilizing JSON with ASP.NET WebForms

Is it recommended to use JSON, JQuery & ASP.NET 2.0 webforms together or is it better suited for MVC with ASP.NET 3.5 & 4.0? When incorporating JSON, should I utilize gridviews and repeaters controls for binding JSON data or should I create custom tables f ...

Develop a Django API for uploading files

I am currently working on a Django application that centers around users uploading files, and I am in the process of creating an API for it. Essentially, my goal is to allow users to send a POST request (using tools like curl, for example) with the file da ...

Gson parser failing to parse JSON data

Below is a JSON string that I am working with: [{"BranchID":1,"SecurityCode1":13,"SecurityCode2":14,"PrintHeight":10,"PrintWidth":10,"Active":true}] This is the code snippet I am using to parse the JSON data: Type t = new TypeToken<List<Setting&g ...

How can the syntax of a JSON file be verified in Unix?

How can I verify the syntax of a JSON file in Unix and then automatically move any invalid files into an error folder? Thank you, Kavin ...

The @JsonInclude(JsonInclude.Include.NON_NULL setting is ineffective for double values

I need to retrieve {"status":201} when route!=0, but I'm currently getting {"status":201,"distance":0.0}. How can I achieve this using @JsonInclude or Jackson without including "distance":0.0? SDBean bean = new SDBean(); if (routeD != 0) { // Ins ...

Retrieve a JSON object from a JSON array using a specific key

I am facing a situation where I have a json array containing multiple json objects. Let's take the example of a course object with the following structure: {"name": "Math", "unit": "3"} The json array looks something like this: [{"name": "Math", "u ...

Exploring JSON data in real-time

My goal here is to utilize the variables retrieved from the route to determine which blog to access from the JSON file. The JSON file consists of an array of sections, each containing an array of blogs. Although the code works flawlessly when I manually s ...

Using StringRequest to post Volley with JSONObject in Android platform

I'm currently facing some confusion after asking a question regarding JSON decoding on the PHP side. My focus is on decoding a JSONObject posted from Android Volley using StringRequest. While I successfully created code for inserting data into MySql, ...

The server-side script using CURL is unable to receive the posted values

My application utilizes php and CURL for sending data to a server using the curl post method. I am facing an issue where the json string that is being posted contains values like "state":"jammu & Kashmir". However, when I attempt to retrieve this dat ...

Storing a JSON array in a MySQL (5.6) database - Which data type to use?

In MySQL version 5.6, when the JSON data type is not available, what would be the most suitable data type for storing a JSON encoded array? Currently considering using either TEXT or VARCHAR. Is this the correct approach for storing JSON data in this scen ...

What is the best way to retrieve JSON data in a React application?

useEffect(async () => { const fetchPostData = async () => { const response = await axios("") setPosts(response.data) } fetchPostData(); }, []) Rendering : posts.map(post => <li>{post.name} ...

Looking for a simple method to link JSON data to an svg element through Javascript?

Looking to harness the power of SVG for a basic graph creation, but uncertain about the process of assigning JSON data dynamically using a 'for loop' to draw rectangles within SVG. Seeking guidance on setting up 1 loop to assign each value for dr ...

No data in Django json request.body

I'm currently working with an API that requires sending a callback to a specific URL. I have configured my URL and view as follows: def get_callback(request): ... some processing with request.body Despite setting up the view, when I check th ...

How can I convert an Array into a Dictionary using JavaScript?

Is there a clever method (perhaps using a map function) to restructure my data object from this: [ {id: 1, from: "1/1/2021", to: "1/2/2022"}, {id: 2, from: "1/3/2021", to: "1/4/2022"}, {id: 1, from: "1/5/2 ...