代码之家  ›  专栏  ›  技术社区  ›  Ethan Leonard

当输入为JSON时,在Julia中发布API

  •  0
  • Ethan Leonard  · 技术社区  · 3 年前

    我正在尝试访问TexSmart HTTP API https://ai.tencent.com/ailab/nlp/texsmart/en/api.html ,其中输入文本是与以下结构匹配的词典:

    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)
                         )
    

    “options”和“echo_data”字段是可选的,可能对我的实现并不重要,所以可以将它们排除在您的答案之外。

    我将如何在HTTP.request中提交此内容?


    基于API页面上的Python示例,我尝试了以下操作:

    HTTP.request("POST", "https://texsmart.qq.com/api", 
    [codeunits(json(Dict("str"=>"He stayed in San Francisco.")))])
    

    我没有收到回复,也不知道为什么。以下是API文档中的Python示例:

    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)
    

    json.dumps().encode() 函数返回 bytes ,我想 codeunits() 包装输入字符串的json表示会起到作用,但仍然没有效果。我没有收到错误,只是收到一个内容长度为0的响应。


    注意:如果有人知道如何使用 PyCall ccall() ,这个答案也适用于我!

    1 回复  |  直到 3 年前
        1
  •  1
  •   mortenpi    3 年前

    以下内容应该有效:

    import HTTP, JSON
    HTTP.request(
        "POST",
        "https://texsmart.qq.com/api",
        [],
        JSON.json(Dict("str"=>"He stayed in San Francisco."))
    )
    

    特别是:

    • 这个 third argument to HTTP.request 是请求头,而不是正文,所以需要在那里传递一个空数组。
    • codeunits 是不必要的,因为 HTTP.request 很高兴也接受了一个字符串。
    推荐文章