Julia中的Post API,当输入为JSON时

huangapple go评论66阅读模式
英文:

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, since HTTP.request happily accepts a string too.

huangapple
  • 本文由 发表于 2023年5月22日 10:33:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定