Indonesia
Asked — Edited
Resolved Resolved by DJ Sures!

How To Send JSON & Set Header Request On Net.Httppost() Method?

Hi everyone, I'm using Net.hTTPPost(url, postData) method on ARC script (JavaScript). I've just realized that this method only send the body payload request as x-www-form-urlencoded. My problems are:

  1. How to send JSON data? I've tried JSON.stringify() but it didn't work as my expectation.
  2. How to set its header request? Here I attach my basic code to make a simple POST request to JSON Placeholder Fake API. I'm curious how to send JSON & how to set its header request.
var resPost = Net.hTTPPost(
   "https://jsonplaceholder.typicode.com/posts";,
   'name=Andy&age=20'
)
print(resPost)

resPost = JSON.parse(resPost)
print(resPost.name)
print(resPost.age)

Run this code & you will see the following log in console:

> {
  "name": "Andy",
  "age": "20",
  "id": 101
}
> Andy
> 20

Hope someone in this community can help me solve this problem. Thanks in advance.



Related Hardware (view all EZB hardware)
EZ-B Iotiny by EZ-Robot
Control IoTiny with Synthiam ARC: 100MHz Cortex‑M4 Wi‑Fi IoT/robot controller-camera, servos, ADC, I2C and digital I/O for easy internet automation
Wi-Fi / USB
Servos 8
Camera
Audio
UART
I2C
ADC
Digital

Related Robot Skills (view all robot skills)
Script by Synthiam
Multi-language ARC Script: build, run and debug Blockly, JavaScript, EZ-Script or Python with Intellisense, run/save/load and Roboscratch support
Real-time EZ-Script console for ARC: enter and execute single-line commands, view output, clear display, and recall history with UP/DOWN.

ARC Pro

Upgrade to ARC Pro

Experience early access to the latest features and updates. You'll have everything that is needed to unleash your robot's potential.

Author Avatar
PRO
Synthiam
#9  

Great! there’s so much power by creating custom robot skills. And the ARC framework exposes methods that make it so easy:)