send JSON Request through HTTP Post

25 ビュー (過去 30 日間)
Ahmed
Ahmed 2022 年 8 月 20 日
回答済み: Swastik Sarkar 2024 年 8 月 2 日
Hi everyone, I am new to the concept of HTTP Post and JSON Requests. I am using CREO | SON a program that can help me control an application using MATLAB. It has a lot of JSON Commands that are sent to my local server through the following URL: http://localhost:9055/creoson .
An Example of one of those JSON requests is
{
"command": "connection"
"function": "connect"
}
Can someone help me send this JSON Request through to my server
  1 件のコメント
Geoff Hayes
Geoff Hayes 2022 年 8 月 31 日
@Ahmed - you may want to start with write JSON object to get an idea as to how to construct the command to send the JSON payload via webwrite.

サインインしてコメントする。

回答 (1 件)

Swastik Sarkar
Swastik Sarkar 2024 年 8 月 2 日
Hi @Ahmed,
Understanding how to make a POST request is essential for interacting with APIs and sending data to servers. This example will help you grasp the basics, which you can then adapt to your specific use case.
Here’s an example of making a POST request to an example server on the internet:
url = 'https://jsonplaceholder.typicode.com/posts'
url = 'https://jsonplaceholder.typicode.com/posts'
jsonPayload = struct('command', 'connection', 'function', 'connect');
jsonStr = jsonencode(jsonPayload)
jsonStr = '{"command":"connection","function":"connect"}'
options = weboptions('MediaType', 'application/json');
response = webwrite(url, jsonStr, options)
response = struct with fields:
command: 'connection' function: 'connect' id: 101
You may find more about using web interfaces in MATLAB from here:
I hope this example provides a clear starting point for making web requests.

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by