小白问题,为啥是红色的呀?
我正在使用翻译帮助我用中文回答你。请原谅任何错误。
您是否在上面定义了 “Coin” 或包含 “template”
定义了“Coin”?
Hi @yuan_yan , please try to post your questions in English, we don’t have many native speakers of Mandarin on this forum.
To really diagnose your issue, you’d have to post the error message that you get when hovering over the Coin keywords. It may simply be that you have not yet defined a template Coin.
非常感谢您的回复,可以给我一个你的网络联系方式吗?实时的那种,我看你的文档也是翻译软件,我现在想学习,但是英文的确实理解不了。希望你能在线帮我看一下。
Steve Seow via Daml Developers Community <daml@discoursemail.com> 于2021年12月13日周一 16:25写道:
I need to learn from you the calling method of JSON API. Can you give me an example? Format write one
Bernhard via Daml Developers Community <daml@discoursemail.com> 于2021年12月13日周一 16:50写道:
I recently created a learning series on YouTube. Take a look at this episode and see if it helps: Daml 101: BYOF: Bring Your Own Frontend with JSON API [2021] - YouTube
I still don’t quite understand your tutorial. Can you help me write a real example? It’s best to annotate.
Steve Seow via Daml Developers Community <daml@discoursemail.com> 于2021年12月13日周一 20:43写道:
Hi @yuan_yan and welcome to the Daml Developer forum!
Full docs for the JSON API can be found here where you can see how to form requests and what the responses look like.
This browser-based interactive tutorial shows how you can add a UI feature with React that ends up calling the JSON API (as part of our frontend library).
Hope this helps

Why is it prompted that the service cannot be found
Nemanja via Daml Developers Community <daml@discoursemail.com> 于2021年12月14日周二 17:30写道:
Change the url to url = 'http://127.0.0.1:7575/v1/create'
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=757): Max retries exceeded with url: /v1/create/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001F932B284C0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))
type or paste code here
yes, I had the wrong port number
so the url is url = http://127.0.0.1:7575/v1/create
It should have Bearer in it, so Authorization: Bearer your_token (see here)
You are missing the protocol in your url: http://.
What is that requests library you are using? It seems odd to me that requests.post takes just two parameters for url and headers. A POST usually has data that you are posting, ie I’d expect to have to supply at least a url, data, and then optionally headers.
The structure of a request to create using the standard fetch API would look like this:
fetch("http://127.0.0.1:7575/v1/create", {
"headers": {
"authorization": "Bearer TOKEN",
"content-type": "application/json",
},
"body": "{\"templateId\":\"TEMPLATEID\",\"payload\":PAYLOAD}",
"method": "POST",
});
My goal now is to request the content in the example. How should I write the request format?
Bernhard via Daml Developers Community <daml@discoursemail.com> 于2021年12月15日周三 18:12写道:
@yuan_yan
I see a number of problems with your sample code.
- The second positional argument of the post method of the requests library is “data”, not “headers”. It represents the body of the HTTP request. So, the headers that you provide in the variable named “header” are passed as the body of the HTTP request, not as the headers. This is why the request comes back with HTTP status 401, as it is missing the Authorization header. To avoid this kind of problem I recommend using kwargs rather than positional arguments, e.g. requests.post(url=url, data=data, headers=headers).
- You need to provide the body of the request, which contains the data you want to pass to the Daml action. E.g. to create of a Coin contract you need to provide issuer, owner, amount and delegates in the request body. See Create a new Contract section in HTTP JSON API Service documentation.
- The headers in your request are malformed. The information required to authorize your request (such as ledger and party represented by ledgerId and actAs parameters) is encoded in JWT. You have to provide this information when you obtain the token. But you don’t have to explicitly include it in the headers of your HTTP request. HTTP JSON API Service parses the authorization parameters out of JWT. In other words it does not look at any headers other than Authorization. Adding ledgerId and actAs headers to your request has no effect. Including these headers in your request will not make the request fail. But it shows that you don’t yet have a good grasp of how the authorization is performed. I suggest you read the section titled Party-specific Access Tokens in HTTP JSON API Service documentation.
Here’s a complete code sample for you. Note that the value of templateId parameter in the HTTP request body must be provided as “<package ID>:<module>:<entity>” or as “<module>:<entity>”. In my example below the value of templateId is “Main:Coin”, where Main is the name of the Daml module and Coin is the name of the template.
import requests
url = "http://localhost:7575/v1/create"
headers = {"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwczovL2RhbWwuY29tL2xlZGdlci1hcGkiOnsibGVkZ2VySWQiOiJNeUxlZGdlciIsImFwcGxpY2F0aW9uSWQiOiJIVFRQLUpTT04tQVBJLUdhdGV3YXkiLCJhY3RBcyI6WyJBbGljZSJdfX0.34zzF_fbWv7p60r5s1kKzwndvGdsJDX-W4Xhm4oVdpk"}
data = '{"templateId": "Main:Coin", "payload": {"issuer": "Alice","owner": "Alice","amount": "999.99","delegates": []}}'
res = requests.post(url, data=data, headers=headers)
print (res.status_code)
print (res.text)





