curl send file as post request - http

I have file with request data:
POST /exampleUrl HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 57907
Content-Type: multipart/form-data; boundary=--
Accept: */*
---
Content-Type: application/octet-stream
Content-Disposition: form-data; name='wanted'; filename=NameOfFile
Content-Transfer-Encoding: binary
[DATA]
---
Content-Type: application/octet-stream
Content-Disposition: form-data; filename=ID1
Content-Transfer-Encoding: binary
[DATA]
-----
How can i send it through curl as post request?

Related

Sendgridr, HTTP/1.1 400 Bad Request in R

i try use sendgridr from this topic How I can add cc when sending email from R with SendGrid
and I have problem with HTTP error.
in the result I've got:
POST /v3/mail/send HTTP/1.1
Host: api.sendgrid.com
User-Agent: libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2
Accept-Encoding: deflate, gzip
Accept: application/json, text/xml, application/xml, */*
Authorization: Bearer SG.Iu.............xxxx....
Content-Type: application/json
Content-Length: 470
>> {"personalizations":[{
>> "to": [ {"email": "mymail#mail.com"}],
>> "cc": [ {"email": "mymail#mail.com"}]
>> }],
>> "from": {"email": "mymail#mail.com"},
>> "subject": "Testing Sendgrid",
>> "content": [{"type": "text/plain", "value": "
>> Dear friend,
>>
>>
>>
>> I'm testing email.
>>
>>
>>
>> Kind regards,
>>
>> Mati"}]
>> }
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 29 Jun 2022 09:13:15 GMT
Content-Type: application/json
Content-Length: 63
Connection: keep-alive
Access-Control-Allow-Origin: https://sendgrid.api-docs.io
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
Access-Control-Max-Age: 600
X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
Strict-Transport-Security: max-age=600; includeSubDomains
Kind regards
Mat

HTTP Post multipart/form-data: Invalid Content-Disposition value

I am working on a HTTPS POST request which contains two multipart/form-data entries.
But for whatever reason I can't get it working.
This is the request I am sending:
POST /my/api/endpoint HTTP/1.0
Host: myserver.de
Content-Type: multipart/form-data; boundary=123456
Content-Length: 147
Connection: close
X-API-KEY: 123
--123456
Content-Disposition: form-data; name="edf"
EDF
--123456
Content-Disposition: form-data; name="parameters"
PARAMETERS
--123456--
What is not shown above that the string is null-terminated ('\0' after --123456--).
But the response I get is:
HTTP/1.1 400 Bad Request
Server: nginx/1.21.3
Date: Wed, 04 May 2022 06:35:34 GMT
Content-Type: application/json; charset=utf-8
Connection: close
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-0c7bdc085f3f2543aee5f677554f0568-3911d802790ef040-00","errors":{"":["Failed to read the request form. Form section has invalid Content-Disposition value: "]}}
Is someone able to determine what I am doing wrong here?
Edit:
If I remove the null termination I get
HTTP/1.1 400 Bad Request
Server: nginx/1.21.3
Date: Wed, 04 May 2022 06:44:43 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
122
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-504d94c952c60246b96667d6a8c2e690-d160644f50acf14a-00","errors":{"edf":["The EDF field is required."],"parameters":["The Parameters field is required."]}}
0
Do you notice the 122 and 0 numbers - where are they comming from?

JSON-Encoded POST/PATCH data in R

I want to make a POST to a webserver running a Rails application (I didn't develop the application, I just need to post to it) and I've been trying to use R with little success.
I've been doing this with httr and jsonlite:
install.packages("httr");
install.packages("jsonlite")
library(jsonlite)
library("httr");
url = "http://192.168.0.168:3000/api/styles";
b <- list(name="Lorem", description="Ipsum");
POST(url,body=b,encode='json');
Which nets me the result of actually being able to post but renders a warning from Rails:
Response [http://192.168.0.168:3000/api/styles/18]
Date: 2015-11-06 13:20
Status: 411
Content-Type: text/html; charset=ISO-8859-1
Size: 307 B
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD><TITLE>Length Required</TITLE></HEAD>
<BODY>
<H1>Length Required</H1>
WEBrick::HTTPStatus::LengthRequired
<HR>
<ADDRESS>
WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13) at
192.168.0.168:3000
All other attempts at using other encoding options or content-type headers have given me a warning of 422 Unprocessable Entity.
I have however been successful at doing this in Python with this snippet:
import requests
url = "http://192.168.0.168:3000/api/styles"
payload = {'name': 'Lorem', 'description': 'Ipsum'}
r = requests.post(url, json=payload)
Can anyone shine a light on what am I doing wrong in the R request?
Update 1: I've added the Verbose Option and the output
-> POST /api/styles HTTP/1.1
-> Host: 192.168.0.168:3000
-> User-Agent: libcurl/7.43.0 r-curl/0.9.3 httr/1.0.0
-> Accept-Encoding: gzip, deflate
-> Cookie: request_method=POST
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/json
-> Content-Length: 38
->
>> {"name":"Lorem","description":"Ipsum"}
<- HTTP/1.1 302 Found
<- X-Frame-Options: SAMEORIGIN
<- X-Xss-Protection: 1; mode=block
<- X-Content-Type-Options: nosniff
<- Location: http://192.168.0.168:3000/api/styles/19
<- Content-Type: text/html; charset=utf-8
<- Cache-Control: no-cache
<- X-Request-Id: b5bbef5f-a627-424d-bbaf-892bf8aac758
<- X-Runtime: 11.910362
<- Server: WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13)
<- Date: Fri, 06 Nov 2015 15:31:48 GMT
<- Content-Length: 105
<- Connection: Keep-Alive
<-
-> POST /api/styles/19 HTTP/1.1
-> Host: 192.168.0.168:3000
-> User-Agent: libcurl/7.43.0 r-curl/0.9.3 httr/1.0.0
-> Accept-Encoding: gzip, deflate
-> Cookie: request_method=POST
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/json
->
<- HTTP/1.1 411 Length Required
<- Content-Type: text/html; charset=ISO-8859-1
<- Server: WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13)
<- Date: Fri, 06 Nov 2015 15:31:48 GMT
<- Content-Length: 307
<- Connection: close
<-
Response [http://192.168.0.168:3000/api/styles/19]
Date: 2015-11-06 15:31
Status: 411
Content-Type: text/html; charset=ISO-8859-1
Size: 307 B
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD><TITLE>Length Required</TITLE></HEAD>
<BODY>
<H1>Length Required</H1>
WEBrick::HTTPStatus::LengthRequired
<HR>
<ADDRESS>
WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13) at
192.168.0.168:3000

Calculating Content Length in multipart HTTP bodies

I am creating a HTTP POST message, and I have a dobut regarding the content length value.
Say my body is as shown below:
POST http://somelink HTTP/1.1
Date: Mon, 22 Feb 1857 12:27:07 GMT
Content-Length: 21797487
Content-Type: multipart/form-data; boundary=---------------------------boundary
-----------------------------boundary
Content-Type: text/plain
Content-Disposition: form-data; name="tid"
someid
-----------------------------boundary
Content-Type: image/jpeg
Content-Disposition: form-data; filename="image.jpeg"; name="File"
SomeRandomtext
-----------------------------boundary--
Should the content length include the \r\n after boundary-- or it should end at boundary--
From the protocol point of view, the payload type doesn't matter. The content length is the length of the message that follows the header block, that's it.

Help, don't know what's wrong with my HTTP multipart POST

POST /upload HTTP/1.1
Host: assets.drop.io
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2
009042316 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Content-Length: 5728
Content-Type: multipart/form-data; boundary=--------MalolHCBdarysNYMHZbIvjbD
--------MalolHCBdarysNYMHZbIvjbD
Content-Disposition: form-data; name="api_key"
0b8a12109c3a1bfc4ba94aec926e1f9cfd8bb6f0
--------MalolHCBdarysNYMHZbIvjbD
Content-Disposition: form-data; name="drop_name"
4pgvoxc
--------MalolHCBdarysNYMHZbIvjbD
Content-Disposition: form-data; name="version"
2.0
--------MalolHCBdarysNYMHZbIvjbD
Content-Disposition: form-data; name="rachel"; filename="rachel"
Content-Type: application/octet-stream
BMv¶
--------MalolHCBdarysNYMHZbIvjbD--
The response I get is: Bad Request. And in the HTML of the response there is this:
:MultiPartParseError: bad content body:
'
----------MalolHCBdarysNYMHZbIvjbD' should == '----------MalolHCBdarysNYMHZbIvjbD
'></td></tr>
The answer is in your question. The response says that the divider should be '----------MalolHCBdarysNYMHZbIvjbD ' instead of ' ----------MalolHCBdarysNYMHZbIvjbD'
Look at the leading and trailing spaces.
The schematic syntax of a multipart/form-data message is as follows:
Content-Type: multipart/form-data; boundary=boundary
--boundary
Content-Disposition: form-data; name="field 1"
data 1
--boundary
Content-Disposition: form-data; name="field 2"
data 2
⋮
--boundary
Content-Disposition: form-data; name="field N"
data N
--boundary--
So the inner part boundaries are --boundary (-- followed by the boundary value) and the last is --boundary-- (-- followed by the boundary value followed by --).
In your case you are just missing the leading --.

Resources