post a csv file to url - r

I am trying to post a .csv file to a url and it works when I am using Python with this code :
import requests
url = 'http:...'
files = {'file': open('test.csv')}
response = requests.post(url, files=files)
Since all the other code is in R and I would like to have all the code at one place I tried to translate it. I tried several different things:
library(httr)
POST("http:...",
body = list(name = "test.csv",
filedata = upload_file("~/test.csv", "text/csv")))
POST("http:...",
body = list(testFile = "~/test.csv"))
POST("http:...",
body = upload_file("~/test.csv"))
But I keep on running into the same error.
Error in curl::curl_fetch_memory(url, handle = handle) : Timeout
was reached
Is there any other way I could try to upload the file to the url using R?
Any help or suggestions are appreciated!

Related

Passing files in POST using Requests in Python

I'm getting below error when making a requests call post method
{'detail': [{'loc': ['body', 'files'], 'msg': 'field required', 'type': 'value_error.missing'}]}
I tried
response = requests.post("url",headers={mytoken},params=p,files=files)
files = { "file 1": open("sample.pdf",'rb'), "file 2":open("sample11.pdf",'rb')}
I want to get 200 status but I'm getting 422 validation error. Any Idea Why? Its for API Testing purpose, Im new to this I've been debugging this for whole day but still couldn't figure out.
It is not clear from the question what kind of a request the server is expecting. Also, its not clear the exact code snippet you are using too.
From the question, the snippet looks like as follows,
response = requests.post("url",headers={mytoken},params=p,files=files)
files = { "file 1": open("sample.pdf",'rb'), "file 2":open("sample11.pdf",'rb')}
if so, that means you are reading files after you send the request, may be thats why the server complained about missing files field.
See the below example on how you can send two files to an endpoint expecting files.
import requests
import logging
logger = logging.getLogger(__name__)
post_url = "https://exampledomain.local/upload"
file1 = open("sample1.pdf", "rb")
file2 = open("sample2.pdf", "rb")
files = {"file1": file1, "file2": file2}
headers = {"Authorization": "Bearer <your_token_here>"}
params = {"file_type": "pdf"}
response = requests.post(post_url, files=files, headers=headers, params=params)
file1.close()
file2.close()
logger.debug(response.text)

Download an excel file behind login (rvest)

I am trying to download excel files that are stored behind an intranet that requires a login. All's working well except when I try to download the file. I am using rvest.
The example below uses dummy data:
my_intranet = "www.mywebsite_login.com"
the_excel_file = "www.mywebsite_login.com/excel.xlsx"
session = session(my_intranet)
form = html_form(session)[[1]]
fl_fm = html_form_set(form, sUserName = "XXXXXX", sPassword = "XXXXXXX")
main_page = session_submit(session, fl_fm)
b = session_jump_to(main_page, the_excel_file)
writeBin(b, basename(the_excel_file))
When I execute:
b = session_jump_to(main_page, the_excel_file)
I get the following curl error:
Error in curl::curl_fetch_memory(url, handle = handle) :
Could not resolve host: NA
Any idea about what's wrong? Thanks!

R - Translate GET requests in Postman with body raw to GET requests in R

I have a question about to add body to GET request.
In Postman, I easily add body raw json to tab Body
In R, I use httr::GETand I can not find any option to add them. I try to use option query, but it return error 500 "Missing parameter username"
This is my code (sorry about fake data):
library(httr)
api <- ".........................../users/authorize"
query <- list("username": "xxxxx", "password": "xxxxxxxxxxxx")
resp <- GET(api, query = query)
stop_for_status(resp)
# Error: Internal Server Error (HTTP 500).
content(resp, type = "text", encoding = "utf-8")
# [1] "{\"status\":\"0\",\"error_code\":\"S002\",\"errors\":\"Missing param [username].\"}"
Can anybody help me to this case? Thank you so much.
Try this:
resp <- httr::POST(body = query, encode = "json")

Can't Move DriveItem in Mocrosoft Graph

I am using Python to download PDF files from OneDrive to a local folder, and also moving the files to a different folder in OneDrive after they have been downloaded.
I am able to download the files from OneDrive to a local folder, however, I get a 400 response when trying to move (PATCH) the files to another OneDrive Folder.
Here is my successful code to download the files:
download_url = 'https://graph.microsoft.com/v1.0/me/drive/items/{item-id}/content'
headers = {'Authorization': 'Bearer ' + json_response['access_token']}
download_url_data = requests.get(download_url, headers=headers)
with open('/Users/Name/Folder/file_name, 'wb') as f:
f.write(download_url_data.content)
Here is my unsuccessful PATCH request to move the files:
move_url = 'https://graph.microsoft.com/v1.0/me/drive/items/{item-id}
move_headers = {'Authorization': 'Bearer ' + json_response['access_token'],
'Content-Type' : 'application/json'}
move_body = {'parentReference' : {'id' : '01EV3NG2F6Y2GOVW7775BZO354PUSELRRZ'}}
move_file = requests.patch(move_url, headers=move_headers, data=move_body)
return move_file.status_code
I have followed the documentation here https://learn.microsoft.com/en-us/graph/api/driveitem-move?view=graph-rest-1.0&tabs=http and I have tried different parentReference id's, but no luck.
Please help! Cheers.
What is the response you're getting (the actual content beside the 400 status code)?
I believe that requests.patch should receive it's data as a string, not a dictionary (json).
Try:
move_file = requests.patch(move_url, headers=move_headers, data=json.dumps(move_body))
And of course don't forget to import json

R - posting a login form using RCurl

I am new to using R to post forms and then download data off the web. I have a question that is probably very easy for someone out there to spot what I am doing wrong, so I appreciate your patience. I have a Win7 PC and Firefox 23.x is my typical browser.
I am trying to post the main form that shows up on
http://www.aplia.com/
I have the following R script:
your.username <- 'username'
your.password <- 'password'
setwd( "C:/Users/Desktop/Aplia/data" )
require(SAScii)
require(RCurl)
require(XML)
agent="Firefox/23.0"
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
curl = getCurlHandle()
curlSetOpt(
cookiejar = 'cookies.txt' ,
useragent = agent,
followlocation = TRUE ,
autoreferer = TRUE ,
curl = curl
)
# list parameters to pass to the website (pulled from the source html)
params <-
list(
'userAgent' = agent,
'screenWidth' = "",
'screenHeight' = "",
'flashMajor' = "",
'flashMinor' = "",
'flashBuild' = "",
'flashPatch' = "",
'redirect' = "",
'referrer' = "http://www.aplia.com",
'txtEmail' = your.username,
'txtPassword' = your.password
)
# logs into the form
html = postForm('https://courses.aplia.com/', .params = params, curl = curl)
html
# download a file once form is posted
html <-
getURL(
"http://courses.aplia.com/af/servlet/mngstudents?ctx=filename" ,
curl = curl
)
html
But from there I can tell that I am not getting the page I want, as what is returned into html is a redirect message that appears to be asking me to login again (?):
"\r\n\r\n<html>\r\n<head>\r\n <title>Aplia</title>\r\n\t<script language=\"JavaScript\" type=\"text/javascript\">\r\n\r\n top.location.href = \"https://courses.aplia.com/af/servlet/login?action=form&redirect=%2Fservlet%2Fmngstudents%3Fctx%3Dfilename\";\r\n \r\n\t</script>\r\n</head>\r\n<body>\r\n Click here to continue.\r\n</body>\r\n</html>\r\n"
Although I do believe there are a series of redirects that occur once the form is posted successfully (manually, in a browser). How can I tell the form was posted correctly?
I am quite sure that once I can get the post working correctly, I won't have a problem directing R to download the files I need (online activity reports for each of my 500 students this semester). But spent several hours working on this and got stuck. Maybe I need to set more options with the RCurl package that have to do with cookies (as the site does use cookies) ---?
Any help so much appreciated!! I typically use R to handle statistical data so am new to these packages and functions.
The answer ends up being very simple. For some reason, I didn't see one option that needs to be included in postForm:
html = postForm('https://courses.aplia.com/', .params = params, curl = curl, style="POST")
And that's it...

Resources