2016-06-26 55 views
1

你好,我想創建和問題張貼到管理平臺API藥劑:如何張貼在休息API(管理平臺)與httpotion

def createIssue(issue) do 
    value = %{"project_id" => 1, "subject" => "Example", "priority_id" => 1, "tracker_id" => 1} 
    content = Poison.Encoder.encode(value, []) 
    HTTPotion.post("#{url}/issues.json", [body: "issue=#{content}" , basic_auth: auth()]) 
end 

上面的代碼的結果如下

Started POST "/issues.json" for 127.0.0.1 at 2016-06-26 11:47:25 +0200 
Processing by IssuesController#create as JSON 
Parameters: {"issue"=>"{\"tracker_id\":1,\"subject\":\"Example\",\"project_id\":1,\"priority_id\":1}"} 
Current user: admin (id=1) 

從管理平臺正確的反應應該是,據我可以瞭解以下

Parameters: {"issue"=>{"tracker_id":1,"subject": "Example","project_id":1,"priority_id":1}} 

任何人都可以幫助嗎? 謝謝

+0

你之前是否從另一種語言調用了此API?如果是,你使用了什麼代碼? – Dogbert

+0

嘗試在'mix.exs'中添加'plug'作爲依賴項,如果您還沒有,請嘗試下面的代碼:https://gist.github.com/anonymous/e532956e00c9630b823fe4434cbe9dec我將發佈解釋爲回答它是否適合你。 – Dogbert

+0

謝謝你,Dogbert。我試圖通過引用/不引用來做類似的事情。你的要點確實奏效!我會在您的github存儲庫中記錄您的信息,並在發佈時將您的解釋標記爲答案。謝謝 –

回答

2

問題不是HTTPotion。您沒有將「問題」節點編碼爲JSON。

def createIssue(issue) do 
    content = Poison.encode!(%{"issue" => %{"project_id" => 1, "subject" => "Example", "priority_id" => 1, "tracker_id" => 1}}) 
    HTTPotion.post("#{url}/issues.json", [body: content, basic_auth: auth()]) 
end 
+0

如果這樣可行,我覺得奇怪的是''issue =#{json}「'解碼爲'{」issue「:json}''。它應該拋出一個錯誤或其他東西,如果它期望整個身體是有效的JSON。 – Dogbert

+0

@Dogbert:它不會拋出錯誤 –

+0

我的Elixir代碼可以在這裏找到[link](https://github.com/wuatanabe/redelix) –