
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.
Run this code & you will see the following log in console:Code:
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)
Hope someone in this community can help me solve this problem. Thanks in advance.Code:
> {
"name": "Andy",
"age": "20",
"id": 101
}
> Andy
> 20
Upgrade to ARC Pro
Get access to the latest features and updates before they're released. You'll have everything that's needed to unleash your robot's potential!
The response will be like this:Code:
My expectation is like this:Code:
By the way, how about my 2nd question? Can you tell me how to set header request?Code:
Thanks in advance.
as for a header request. Are you referring to a url request with embedded parameters?
HTTP headers let the client and the server pass additional information with an HTTP request or response. For example if you make a GET request to https://httpbin.org/get you'll get this response:
The headers are Accept, Accept-Encoding, Host, etc. How to set it on ARC?Code:
Thanks in advance
The example for it is here: https://synthiam.com/Support/Create-Robot-Skill/Examples/custom-javascript-extension
Essentially, your robot skill subscribes to the ARC JavaScript OnSetValues event and registers your extension method. It's super simple. Just press the CREATE button to create a template to work from.
The CREATE template will also add a bunch of stuff that you won't need. Such as a ConfigForm and a Configuration class. You can remove those. See the downloadable example in the link above. I posted the Example Project for creating an extension to the JavaScript engine.
That way, you can create a custom ARC JavaScript method for something like...
Code:
Or even better is to allow the method to accept a key/value pair list of header name and header values. Something like that...
Code: