2017-06-29 35 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')