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

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

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:

  1. Dict("str" => "text you want to analyze",
  2. "options"=> Dict( "input_spec"=>Dict("lang"=>"auto"),
  3. "word_seg"=>Dict("enable"=>true),
  4. "pos_tagging"=>Dict("enable"=>true,
  5. "alg"=>"log_linear"),
  6. "ner"=>Dict("enable"=>true,
  7. "alg"=>"fine.std")),
  8. "syntactic_parsing"=>Dict("enable"=>false),
  9. "srl"=>Dict("enable"=>false)),
  10. "echo_data"=>Dict("request_id"=>12345)
  11. )

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:

  1. HTTP.request("POST", "https://texsmart.qq.com/api",
  2. [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:

  1. import json
  2. import requests
  3. obj = {"str": "he stayed in San Francisco."}
  4. req_str = json.dumps(obj).encode()
  5. url = "https://texsmart.qq.com/api"
  6. r = requests.post(url, data=req_str)
  7. r.encoding = "utf-8"
  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:

  1. Dict("str" => "text you want to analyze",
  2. "options"=> Dict( "input_spec"=>Dict("lang"=>"auto"),
  3. "word_seg"=>Dict("enable"=>true),
  4. "pos_tagging"=>Dict("enable"=>true,
  5. "alg"=>"log_linear"),
  6. "ner"=>Dict("enable"=>true,
  7. "alg"=>"fine.std"),
  8. "syntactic_parsing"=>Dict("enable"=>false),
  9. "srl"=>Dict("enable"=>false)),
  10. "echo_data"=>Dict("request_id"=>12345)
  11. )

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:

  1. HTTP.request("POST", "https://texsmart.qq.com/api",
  2. [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:

  1. import json
  2. import requests
  3. obj = {"str": "he stayed in San Francisco."}
  4. req_str = json.dumps(obj).encode()
  5. url = "https://texsmart.qq.com/api"
  6. r = requests.post(url, data=req_str)
  7. r.encoding = "utf-8"
  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

以下应该可以工作:

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

具体来说:

  • HTTP.request 的第三个参数是请求头部,而不是请求体,所以你需要在那里传递一个空数组。
  • codeunits 是不必要的,因为 HTTP.request 也可以接受一个字符串。
英文:

The following should work:

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

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:

确定