I want to use Chinese Word Segmentation and find this API.

sample case work:

URLRead[HTTPRequest["api.ltp-cloud.com/analysis/", <|"Query" -> {"api_key" -> "x1e3E1Z3H4AzgRQolEGE8PxHlCEXtTnDbJvkjw5p", 
                    "text" -> "这是一个简单的报文请求", 
                    "pattern" -> "ws", 
                    "format" -> "plain"}, 
                    "Method" -> "GET"|>]["URL"], "Body", CharacterEncoding -> "UTF-8"]

这 是 一个 简单 的 报文 请求

For adding custom keyworks,we have to build a XML tree.

enter image description here

the text parameter of body is the string object of the XML tree.

I use Postman to simulate this process:

POST http://api.ltp-cloud.com/analysis/ HTTP/1.1
Host: api.ltp-cloud.com
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 6a28f2a0-79f4-af0c-ef5b-23f2c9b03f3f


enter image description here

自定义 分词 结果 的 示例

I use mma to call this API:

xml = Import["https://wolfr.am/nhIeHvVf"];
       <|"Body" -> {"api_key" -> "x1e3E1Z3H4AzgRQolEGE8PxHlCEXtTnDbJvkjw5p", 
                    "text" -> xml, 
                    "pattern" -> "ws", 
                    "format" -> "plain", 
                    "xml_input" -> "true"}, 
        "Method" -> "POST", 
        "Headers" -> <|"Content-Type" -> "application/x-www-form-urlencoded"|>|>]["URL"], 
                               "Body", CharacterEncoding -> "UTF-8"]

It tells me to input username and password, but this is not necessary.

In Fiddler,I find it not sent the body of HTTPRequest because it use GET. (weird!)

enter image description here

How to get the same result as Postman?

  • $\begingroup$ I hit a similar snag the other month: mathematica.stackexchange.com/questions/147526/… and was able to partially figure it out maybe something there will help $\endgroup$ – b3m2a1 Jul 20 '17 at 5:05
  • $\begingroup$ @b3m2a1 what the method have you used? $\endgroup$ – partida Jul 20 '17 at 5:15
  • 1
    $\begingroup$ @b3m2a1 and partida, though in this case is appears to be a simple mistake due to "URL" in HTTPRequest[...]["URL"]. $\endgroup$ – Kuba Jul 20 '17 at 5:20
  • $\begingroup$ I was forced to use URLFetch and so something about passing "Parameters" to it interfered with the "Body" but I hit a similar snag with HTTPRequest. What happens if you pre-export your body to a string? $\endgroup$ – b3m2a1 Jul 20 '17 at 5:21
  • 1
    $\begingroup$ @Kuba I delete the ["URL"] and it can works!!! But before,it throwed an error....... $\endgroup$ – partida Jul 20 '17 at 5:23

Browse other questions tagged or ask your own question.