Add file to multipart form request in IntelliJ HTTP Client - rest

I am attempting to POST an HTTP request to the Mindee API using IntelliJ IDEA's HTTP client. Specifically, I want to upload an image of a receipt. According to the documentation, the form data must have the form
{ file: your_file }
So my question is...how do I specify the field name for a file in IntelliJ's HTTP client? What is the syntax?
I've tried a few different things with similar results. I get
POST https://api.mindee.net/products/expense_receipts/v3/predict
HTTP/1.1 400 BAD REQUEST
Date: Mon, 01 Feb 2021 22:18:15 GMT
Content-Type: application/json
Content-Length: 84
Connection: keep-alive
Server: nginx/1.15.12
Access-Control-Allow-Origin: *
{
"details": {
"file": [
"This field is required."
]
},
"message": "Invalid fields in form"
}
Here are two of my attempts:
POST https://api.mindee.net/products/expense_receipts/v3/predict
Content-Type: multipart/form-data; bundary=boundary
X-Inferuser-Token: <my token>
{
file: < /path/to/image.jpg
}
POST https://api.mindee.net/products/expense_receipts/v3/predict
Content-Type: multipart/form-data; bundary=boundary
X-Inferuser-Token: <my token>
--boundary
Content-Disposition: form-data; name="file"
< /path/to/image.jpg
--boundary--
The second seemed the most promising since it took longer to return a response, but I still got the same thing.

The second attempt request should work okay, but you have a typo in there - bundary instead of boundary.

Related

Dropbox REST API 400 error when uploading file

Recently the dropbox api has been giving me 404 errors when uploading a file. Same code has been working for months. Has anything changed? I'm accessing the rest api through sharpbox.
Error response is below. Part of the request I'm making is below that.
Response:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 28 Mar 2016 13:20:40 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Dropbox-Request-Id: ed356744df5e9541856f727505cceee0
X-Robots-Tag: noindex, nofollow, noimageindex
2e
{"error": {"file": "Expecting a file upload"}}
0
Request:
POST https://api-content.dropbox.com/1/files/dropbox/VMDVGZION?file=test.pdf&oauth_consumer_key=0o1pqhuy0ul766h&oauth_nonce=f74c8207d9a741929529f1a662804e66&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1459171240&oauth_token=0apqmt3p17e6s69z&oauth_version=1.0&oauth_signature=8i0QAVDTIDyQPUMNZTfDWdFDiyM%3d HTTP/1.1
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299
Host: api-content.dropbox.com
Content-Length: 292539
-------------------------------28947758029299
Content-Disposition: form-data; name="file"; filename="test.pdf";
Content-Type: application/octet-stream
%PDF-1.4 %âãÏÓ
..
-------------------------------28947758029299--

Error 500 trying to create Vertex using Rest API

I'm trying to create a Vertex using the Rest API. I've been able to successfully use the Rest API to create a database, and to create classes (but without extending the "V" object).
However, when I try to create a Vertex using the "command" POST, I always get an error 500.
From Fiddler, here is my POST data:
POST http://dbserver:2480/command/G4JTest-8587569988225882445/sql HTTP/1.1
Accept-Encoding: gzip,deflate
Authorization: Basic cm9vdDpkYktpbmc=
Host: dbserver:2480
Content-Length: 105
Expect: 100-continue
Connection: Keep-Alive
{"command": "create class :classname extends V", "parameters": { "classname": "TestCreateVertexVertex"} }
Here's my response:
HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Date: Sun Oct 11 17:01:44 PDT 2015
Content-Type: application/json; charset=utf-8
Server: OrientDB Server v.2.1.2 (build UNKNOWN#r; 2015-09-09 21:41:27+0000)
Connection: Keep-Alive
Content-Length: 349
{
"errors": [{
"code": 500,
"reason": 500,
"content": "com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException: Cannot find a command executor for the command request: sql.{\"command\": \"create class :classname extends V\", \"parameters\": { \"classname\": \"TestCreateVertexVertex\"} }"
}
]
}
Can someone please explain what I'm doing incorrectly?
This was answered in another forum. The JSON post capability is only available in version 2.2. Since I am using 2.1.x, the error is not un-expected ...
Since 2.2 is Alpha, I'll be using the older REST interface for commands.
Or RTFM (more carefully ...)

OneDrive REST API - Upload - Content of .png file converted to jpeg

When using the OneDrive REST API to upload files, the content of some files with a .png extension is automatically converted to JPEG. I'm able to reproduce the problem with both documented upload methods.
Sample png files:
http://www39.zippyshare.com/v/59255310/file.html
http://www2.zippyshare.com/v/11270772/file.html
For reference the shortened requests:
PUT method
PUT https://apis.live.net/v5.0/folder.<removed>/files/i2.png HTTP/1.1
Pragma: no-cache
<removed - no Content-Type header>
Content-Length: 33579
Host: apis.live.net
When the Content-Type in the header of the PUT request is set to application/octet-stream the following error is returned:
HTTP/1.1 415 Unsupported Media Type
Server: Live-API/19.7.925.4009 Microsoft-HTTPAPI/2.0
<removed>
{
"error": {
"code": "request_body_invalid_media_type",
"message": "The Content-Type header 'application/octet-stream' isn't supported."
}
}
POST method
POST https://apis.live.net/v5.0/folder.<removed>/files HTTP/1.1
Content-Type: multipart/form-data; boundary=735b1931-a2bb-4970-8142-373848528fcb
<removed>
Content-Length: 33767
Host: apis.live.net
--735b1931-a2bb-4970-8142-373848528fcb
Content-Disposition: form-data; name="file"; filename="i2.png"
Content-Type: application/octet-stream
.PNG<removed>
--735b1931-a2bb-4970-8142-373848528fcb--
Other OneDrive APIs
The problem does not happen with the API (skyapi.onedrive.live.com/API/2) Microsoft is using on the OneDrive website and in the Windows desktop client (skydrive.exe).
OneDrive developers:
Instead of returning a 415 error in the PUT request, would it be possible to support requests where the Content-Type: application/octet-stream header is set? In this case the data should be left as is without doing any data conversion. Thank you.
This is documented behaviour as per http://msdn.microsoft.com/en-us/library/dn659726.aspx (see the first note under uploading).
If you want to disable conversion, you can set downsize_photo_uploads=false in your query string.

REST Upload to Skydrive without Content-Length

I'm trying to upload a file to Skydrive where I don't a-priori know the Content-Length. With other storage services I can do this with chunked HTTP upload, but Skydrive always complains about Content-Length being invalid.
Here are the full headers I'm sending:
PUT /v5.0/me/skydrive/files/skydrive_test.js?overwrite=ChooseNewName HTTP/1.1
Authorization: Bearer <TOKEN_REDACTED>
host: apis.live.net
content-type: application/javascript
Connection: keep-alive
Transfer-Encoding: chunked
Here's the response I get back:
cache-control: private, no-cache, no-store, must-revalidate
transfer-encoding: chunked
content-type: application/json; charset=UTF-8
server: Live-API/16.4.1731.327 Microsoft-HTTPAPI/2.0
x-content-type-options: nosniff
x-http-live-request-id: API.c6afda25-2d9f-4248-9f49-001ccb3a9007
x-http-live-server: BAYMSG1010836
date: Wed, 15 May 2013 14:33:00 GMT
{ "error": { "code": "request_invalid_content_length",
"message": "The value for the Content-Length header isn't valid." }}
Is there any way I can do this without setting Content-Length (i.e. using chunked encoding)?
I'm using node.js to do this, but it should apply equally with any language using the REST API, hence I haven't tagged this with a particular language.
For example Dropbox offers the Chunked Upload command: https://www.dropbox.com/developers/core/docs#chunked-upload
And Google Drive, even though it says it wants Content-Length, doesn't need it for it's resumable upload API: https://developers.google.com/drive/manage-uploads#resumable
Is there an API I'm missing?
Edit: Things I've tried: Setting Content-Length: 0 results in it working, but the file is zero bytes. Setting Content-length:0 and Transfer-Encoding: chunked, results in the original error above.
Try setting a dummy content-length to see if it is acceptable. Otherwise set the file size.
If you are using node.js you can get the file size you are trying to upload and set the size to content-length in OCTETs. You can get the file size requiring the fs (filesystem) module in node.
var fs = require('fs');
fs.watchFile('some.file', function () {
fs.stat('some.file', function (err, stats) {
console.log(stats.size);
});
});
#Resource

AbsoluteURI support in Play Framework 2.1

As stated here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
To allow for transition to absoluteURIs in all requests in future versions of HTTP, all HTTP/1.1 servers MUST accept the absoluteURI form in requests, even though HTTP/1.1 clients will only generate them in requests to proxies.
I have client which sends POST-requests to my play-2.1.1 server. He sends it this way:
POST http://172.16.1.227:9000/A8%3aF9%3a4B%3a20%3a89%3a40/1089820966/ HTTP/1.1
Content-Length: 473
Content-Type: application/json
Date: Thu, 25 Apr 2013 15:44:43 GMT
Host: 172.16.1.227:9000
User-Agent: my-client
...some data...
All requests are rejected with "Action not found" error. The very same request which I send using curl is just fine and the only difference between them is curl send it with relative URI:
POST /A8%3aF9%3a4B%3a20%3a89%3a40/1089820966/ HTTP/1.1
Accept: */*
Content-Length: 593
Content-Type: application/json
Host: 172.16.1.227:9000
User-Agent: curl/7.30.0
I created the following simple workaround in Global.scala:
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
if (request.path.startsWith("http://")) {
super.onRouteRequest(request.copy(
path = request.path.replace("http://"+request.host, "")
))
} else super.onRouteRequest(request)
}
And with this workaround all requests from my client are handled correctly.
So, is there more straightforward way to do it in Play Framework or thats the only way?
Thanks to #nraychaudhuri Play 2.2 supports absoluteURI-style request headers.
Here's the issue and pull request: https://github.com/playframework/playframework/pull/1060