英文:
Post API in Julia when the input is a JSON
问题
I'm trying to access the TexSmart HTTP API https://ai.tencent.com/ailab/nlp/texsmart/en/api.html , where the input text is a dictionary matching the following structure:
Dict("str" => "text you want to analyze",
"options"=> Dict( "input_spec"=>Dict("lang"=>"auto"),
"word_seg"=>Dict("enable"=>true),
"pos_tagging"=>Dict("enable"=>true,
"alg"=>"log_linear"),
"ner"=>Dict("enable"=>true,
"alg"=>"fine.std")),
"syntactic_parsing"=>Dict("enable"=>false),
"srl"=>Dict("enable"=>false)),
"echo_data"=>Dict("request_id"=>12345)
)
The "options" and "echo_data" fields are optional and probably not crucial to my implementation, so feel free to leave them out of your answer.
How would I submit this in a HTTP.request?
Based on the Python example on the API page, I tried this:
HTTP.request("POST", "https://texsmart.qq.com/api",
[codeunits(json(Dict("str"=>"He stayed in San Francisco."))])
I didn't receive a response, and I'm not sure why. Here's the Python example in the API's documentation:
import json
import requests
obj = {"str": "he stayed in San Francisco."}
req_str = json.dumps(obj).encode()
url = "https://texsmart.qq.com/api"
r = requests.post(url, data=req_str)
r.encoding = "utf-8"
print(r.text)
Since the json.dumps().encode()
function returns bytes
, I thought that codeunits()
wrapping a json representation of the input string would do the trick, but still nothing. I don't receive an error, just a Response with content length 0.
Note: If someone knows a way to do this with PyCall
or ccall()
, that answer would also work for me!
英文:
I'm trying to access the TexSmart HTTP API https://ai.tencent.com/ailab/nlp/texsmart/en/api.html , where the input text is a dictionary matching the following structure:
Dict("str" => "text you want to analyze",
"options"=> Dict( "input_spec"=>Dict("lang"=>"auto"),
"word_seg"=>Dict("enable"=>true),
"pos_tagging"=>Dict("enable"=>true,
"alg"=>"log_linear"),
"ner"=>Dict("enable"=>true,
"alg"=>"fine.std"),
"syntactic_parsing"=>Dict("enable"=>false),
"srl"=>Dict("enable"=>false)),
"echo_data"=>Dict("request_id"=>12345)
)
The "options" and "echo_data" fields are optional and probably not crucial to my implementation, so feel free to leave them out of your answer.
How would I submit this in a HTTP.request?
Based on the Python example on the API page, I tried this:
HTTP.request("POST", "https://texsmart.qq.com/api",
[codeunits(json(Dict("str"=>"He stayed in San Francisco.")))])
I didn't receive a response, and I'm not sure why. Here's the Python example in the API's documentation:
import json
import requests
obj = {"str": "he stayed in San Francisco."}
req_str = json.dumps(obj).encode()
url = "https://texsmart.qq.com/api"
r = requests.post(url, data=req_str)
r.encoding = "utf-8"
print(r.text)
Since the json.dumps().encode()
function returns bytes
, I thought that codeunits()
wrapping a json representation of the input string would do the trick, but still nothing. I don't receive an error, just a Response with content length 0.
Note: If someone knows a way to do this with PyCall
or ccall()
, that answer would also work for me!
答案1
得分: 1
以下应该可以工作:
import HTTP, JSON
HTTP.request(
"POST",
"https://texsmart.qq.com/api",
[],
JSON.json(Dict("str"=>"He stayed in San Francisco."))
)
具体来说:
HTTP.request
的第三个参数是请求头部,而不是请求体,所以你需要在那里传递一个空数组。codeunits
是不必要的,因为HTTP.request
也可以接受一个字符串。
英文:
The following should work:
import HTTP, JSON
HTTP.request(
"POST",
"https://texsmart.qq.com/api",
[],
JSON.json(Dict("str"=>"He stayed in San Francisco."))
)
In particular:
- The third argument to
HTTP.request
are the request headers, not body, so you need to pass an empty array there. codeunits
is unnecessary, sinceHTTP.request
happily accepts a string too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论