Asked
— Edited
Resolved by DJ Sures!
How To Send JSON &Amp; 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:
- How to send JSON data? I've tried JSON.stringify() but it didn't work as my expectation.
- 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 EZ-B IoTiny
Related Controls
Script
EZ-Script Console
like this...
Thanks for your help, DJ Sures. But if I do like your code:
The response will be like this:
My expectation is like this:
By the way, how about my 2nd question? Can you tell me how to set header request? Thanks in advance.
The response is because that’s the response from the server that you’re talking to. The response is unaltered. If it’s not formatted as you expect, that’s a conversation you’ll want to have with the server owner.
as for a header request. Are you referring to a url request with embedded parameters?
Thanks DJ Sures, you're right. It's the response from the server. I'll try another way.
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? Thanks in advance
There's a few ways you can access the .Net framework CLR from the ARC JavaScript engine. While you could use the System.Net namespace and access the methods yourself, it might make more sense to create a custom JavaScript extension with your needs. It's really easy to do, specifically since you have experience. Plus it's re-usable to it assists others.
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...
Or even better is to allow the method to accept a key/value pair list of header name and header values. Something like that...
Oh, and to access the CLR directly for posting your own header information with the .NET libraries, here's an example with the ARC JavaScript engine: https://synthiam.com/Support/javascript-api/net-framework-clr
Ah, because i'm crazy, I wrote it for you. So
Big thanks, Sir! You're awesome! Using this code, you also solved my 1st question. Now I can set the body request content type to application/JSON. Once again thanks for your help. See you on my next questions, Sir hehe.
Great! there’s so much power by creating custom robot skills. And the ARC framework exposes methods that make it so easy