Robot Framework “multipart/form-data” REST request with multiple parameters - python-requests

I am trying to use requestslibrary to upload some files, goal is to achieve this:
------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="files"; filename="photo-2.jpeg"
Content-Type: image/jpeg
------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="fileId"
b3duLWZpbGVzL2ZmZmZmZmZmYTQyNDVmODAvMjAxNTY*
------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="extract"
false
------WebKitFormBoundary61N9vqJ7380nh6iv--
and now I have this, as per this:
${data}= Evaluate {'files': open("C:/testautomation/resources/Assets/photo-2.jpeg", 'r+b'), 'extract': (None, 'false'), 'fileId': (None, 'b3duLWZpbGVzL2ZmZmZmZmZmYTQyNDVmODAvMjAxNTY*')}
log ${data}
${result}= Post Request rest ${url} headers=${HEADERS} files=${data}
I THINK that the only bit I am missing is the "Content-Type: image/jpeg" from the first part, but how on earth I can add that? Currently the file gets uploaded, but it is not considered to be an image file.

The answer was:
${data}= Evaluate {'files': ('photo-1.jpeg', open("C:/testautomation-robot/resources/Assets/photo-1.jpeg", 'r+b'), 'image/jpeg'), 'extract': (None, 'false'), 'fileId': (None, 'b3duLWZpbGVzL2ZmZmZmZmZmYTQyNDVmODAvMjAxNTY*')}
Found an example from here: https://code.i-harness.com/en/q/bcfb9b
>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
In the above, the tuple is composed as follows:
(filename, data, content_type, headers)

Create a python resources and call it through robot framework:
and call it through robot framework
Call params from robotframework:
upload multipart files post request ${headers} ${url} resources/files/upload_file/testfile1_upload.pdf

Related

How to POST multipart/form-data using Vegeta?

I wanted to request POST a image file as multipart/form-data with Vegeta
But when I use this code, it didn't work well. As I thought, 'mean of Bytes In' in Vegeta report needed to be over 20000 because of the image size, but it was just 55.00
I command like this because it is on Windows Power Shell
PS >vegeta attack -duration=10s -rate 100 -targets .\targets_formdata.txt -output output\results.bin
targets_formdata.txt
POST http://url/to/request
Content-Type: multipart/form-data; boundary=Boundary+1234
#body.txt
body.txt
--Boundary+1234
Content-Disposition: form-data; name="file"; filename="DvBp50cVYAEIfxd.jpg"
Content-Type: image/jpeg
I wrote --Boundary+1234 as really it is, as --Boundary+1234, this could be a problem? I don't know what is the real problem.

Convert application/octet-stream content type to string

Getting attachment ZIP file in REST call response and its content type is application/octet-stream. How to convert it as zip file.
Tried fromBase64, but no luck.
Runtime: 4.3
Try using the Compression Module to decompress the zip payload received.
Example:
<compression:extract>
<compression:compressed>#[payload]</compression:compressed>
<compression:extractor>
<compression:zip-extractor/>
</compression:extractor>
</compression:extract>
Used below code to transform and write zip to local drive.
<ee:transform doc:name="Transform Message" doc:id="da388237-d53a-47aa-83b2-96d1ef04489d" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/octet-stream
---
payload.content]]></ee:set-payload>
</ee:message>
</ee:transform>
<file:write doc:name="Write" doc:id="1cfd5210-a65a-4ace-9f07-0f575db3a01a" config-ref="File_Config" path="C:\Retrieval.zip"/>

How do I translate my CLI curl POST api create user call to R httr?

I want to add users to groups on nextcloud via their API.
I succeeded with adding one user (ncuser) to Group1 and Group2 from my CLI, by running this request:
curl -X POST https://adminuser:'admin pass phrase'#cloud.example.org/ocs/v1.php/cloud/users/ncuser/groups -d groupid="Group1" -d groupid="Group2" -H "OCS-APIRequest: true"
With the following response:
<?xml version="1.0"?>
<ocs>
<meta>
<status>ok</status>
<statuscode>100</statuscode>
<message>OK</message>
<totalitems></totalitems>
<itemsperpage></itemsperpage>
</meta>
<data/>
</ocs>
I attempted to run the request in Rstudio with this code:
library(curl)
library(httr)
call <- POST("https://adminuser:'admin pass phrase'#cloud.example.org/ocs/v1.php/cloud/users/ncuser/groups",
body = list(groupid = "Group1", groupid = "Group2"),
add_headers('OCS-APIRequest' = "true", 'Content-Type' = "application/x-www-form-urlencoded"))
But this isn't successful. I get this response in R:
Response [https://adminuser:'admin pass phrase'#cloud.example.org/ocs/v1.php/cloud/users/ncuser/groups]
Date: 2020-03-22 15:43
Status: 401
Content-Type: application/json; charset=utf-8
Size: 140 B
Here is the content of my R POST request:
content(call)
$ocs
$ocs$meta
$ocs$meta$status
[1] "failure"
$ocs$meta$statuscode
[1] 997
$ocs$meta$message
[1] "Current user is not logged in"
$ocs$meta$totalitems
[1] ""
$ocs$meta$itemsperpage
[1] ""
$ocs$data
list()
It seems like I wasn't able to login through the POST() url, and need to add adminuser and 'admin pass phrase' in some other way, but i am not sure how.
Also, the nextcloud API documentation states that OCS-APIRequest and the content type needs to be specified in the header, but I am not sure if i did that correctly.
All calls to OCS endpoints require the OCS-APIRequest header to be set to true.
All POST requests require the Content-Type: application/x-www-form-urlencoded header. (Note: Some libraries like cURL set this header automatically, others require setting the header explicitly.)
How should my POST request be written in R so that I can successfully add my nextcloud users to groups?
Any help would be highly appreciated.
Maybe try
call <- POST("https://cloud.example.org/ocs/v1.php/cloud/users/ncuser/groups",
body = list(groupid = "Group1", groupid = "Group2"),
add_headers('OCS-APIRequest' = "true"),
authenticate("adminuser", "admin pass phrase"),
encode = "form")
This will make sure POST encode the body properly and will more safely pass the username/password. You can also try adding in verbose() to get more output. It's not going to be easy to help you without some sort of reproducible example we can use for testing.

Get data via Netatmo API within R

I tried to get data from a netatmo station I have access to via API. The following code I used in R.
myapp <- oauth_app("my_netatmo",key="my_netatmo_client_id",secret="my_netatmo_client_secret")
ep <- oauth_endpoint(authorize = "http://api.netatmo.net/oauth2/authorize",access = "http://api.netatmo.net/oauth2/token")
sig_tok <- oauth2.0_token(ep,myapp, scope="read_station")
#after that I get redirected to my browser to log in and after that sig_tok contains an access token
sig <- config(token = sig_tok)
html_get<-GET("http://api.netatmo.net/api/devicelist",sig)
html_get contains this:
html_get
Response [http://api.netatmo.net/api/devicelist]
Status: 400
Content-type: application/json; charset=utf-8
{"error":{"code":1,"message":"Access token is missing"}}
What am I doing wrong since "sig" seems to contain a token:
sig$token$credentials$access_token
[1] "5**********************f|3**********************a"
There are two tokens or am I wrong (because of "|" in between)?
The '|' inside the access_token is part of it, it is only one access token.
From the documentation: http://dev.netatmo.com/doc/methods/devicelist,
the parameter name is: "access_token". I don't know the R language, but it seems you are sending "token" as a parameter, and not "access_token". It may explain the issue.

Proper Chunked Transfer Encoding Format

I'm curious about the proper format of chunked data in comparison to the spec and what Twitter returns from their activity stream.
When using curl to try to get a Chunked stream from Twitter, curl reports:
~$ curl -v https://stream.twitter.com/1/statuses/sample.json?delimited=length -u ...:...
< HTTP/1.1 200 OK
< Content-Type: application/json
< Transfer-Encoding: chunked
<
1984
{"place":null,"text":...
1984
{"place":null,"text":...
1984
{"place":null,"text":...
I've written a chunked data emitter based upon the Wikipedia info and the HTTP spec (essentially: \r\n\r\n), and my result looks like this:
~$ curl -vN http://localhost:7080/stream
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Transfer-Encoding: chunked
<
{"foo":{"bar":...
{"foo":{"bar":...
{"foo":{"bar":...
The difference being that it appears that Twitter is including the length of the string as part of the body of the chunk as an integer (in conjunction with the value in Hex that must also be there), and I wanted to make sure that I wasn't missing something. The Twitter docs make no mention of the length value, it's not in their examples, nor do I see anything about it in the spec.
If your code does not emit length information that it is clearly incorrect. See http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.3.6.1.
RCF2616-19.4.6 Introduction of Transfer-Encoding
A process for decoding the "chunked" transfer-coding (section 3.6) can be represented in pseudo-code as:
length := 0
read chunk-size, chunk-extension (if any) and CRLF
while (chunk-size > 0) {
read chunk-data and CRLF
append chunk-data to entity-body
length := length + chunk-size
read chunk-size and CRLF
}
read entity-header
while (entity-header not empty) {
append entity-header to existing header fields
read entity-header
}
Content-Length := length
Remove "chunked" from Transfer-Encoding
As RFC says, the chunk-size will not append to the entity-body. So that is nomal you can not see the chunk-size.And I have read the souce code of curl(function Curl_httpchunk_read)and make sure it skips the chunk-size\r\n, just append chunk-size bytes behind it to body.
The twitter replys with chunk-size,I think it is because of using https, the whole data is encrypted.

Resources