2017-06-29 62 views
1

這是,我認爲,一些非常簡單的捲曲代碼,我試圖將其轉換爲httr格式。- 在httr中的用戶捲曲等效

curl -X POST \ 
    --user '<email>:<password>' \ 
    --header 'user-key: <user_key>' \ 
    --url https://api.m.com/v1/clients 

到目前爲止,我已經嘗試

library(httr)  
POST(url = "https://api.m.com/v1/clients", 
       add_headers('user-key' = "userkey", 
          user = 'email:password')) 

但沒有成功。這裏有什麼錯誤的提示?捲曲代碼中是否有與--user等價的httr?

+2

['curlconverter'](https://github.com/hrbrmstr/curlconverter)可用於捲曲的命令行翻譯成'httr'通話,但你應該使用'authenticate()的'選項在從'--user'傳入憑證時調用'POST'或'GET' – hrbrmstr

回答

3
library(httr) 

username <- 'my_user_name' 
password <- 'my_password' 

POST(url = "https://api.m.com/v1/clients", 
       config = authenticate(username, password), add_headers("Content-Type: application/json"), 
       body = upload_file('./my_file.json'), 
       encode = 'json')