Skip to content

Commit

Permalink
Added the possibility to send bytes from a file as body in a POST by …
Browse files Browse the repository at this point in the history
…specifying a reference to the file in the body.

This body (with the reference) will be replaced by the file-bytes on the fly.
  • Loading branch information
pweerd committed Aug 26, 2023
1 parent 40c0fad commit 18409be
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
12 changes: 12 additions & 0 deletions help.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ If the request is executed, the saveset (see later) is saved. Since a saveset ho

The native response might be changed by 1 or more response plugins. See later.

If you want to send file bytes in a POST/PUT/DELETE command, it is possible to reference the filename in the body. WebCurl will intercept the reference, read the requested file and send the file-bytes as body instead. The syntax is like this:
```
{
"_file_body": {
"file": "some file",
"type": "some mimetype"
}
}
```
For common files like images, the mime-type is internally known and you can omit the 'type'.




### Server types
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/nl/bitmanager/webcurl/AjaxHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@
package nl.bitmanager.webcurl;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;

import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Method;
import fi.iki.elonen.NanoHTTPD.Response.IStatus;
import nl.bitmanager.core.Invariant;
Expand Down Expand Up @@ -63,8 +71,8 @@ static AjaxResult execute (Settings settings, Method method, String url, byte[]

Endpoint ep = settings.getEndpointFor(url);
Request.Builder bldr = ep.createRequestBuilder(url);
RequestBody body = bytes == null ? null : RequestBody.create(jsonMediaType, bytes);
int bodyLen = bytes==null? 0: bytes.length;
RequestBody body = createRequestBody(bytes);
int bodyLen = body==null? 0: (int)body.contentLength();
switch (method) {
case GET: break;
case DELETE:
Expand Down Expand Up @@ -102,6 +110,29 @@ static AjaxResult execute (Settings settings, Method method, String url, byte[]
return new AjaxResult(ep, response);
}
}

/**
* Transform the array of bytes into a RequestBody
* Eventual convert a _file_body hash into an array of bytes by reading the supplied filename
*/
private static RequestBody createRequestBody (byte[] body) throws IOException {
if (body==null) return null;

JsonNode bodyNode = JsonHelper.bytesToJsonNode(body);
ObjectNode fileNode = JsonHelper.readObject(bodyNode, "_file_body", null);
if (fileNode != null) {
JsonNode fn = fileNode.get("file");
if (fn != null) {
Path p = Paths.get(fn.asText());

byte[] fileBytes = Files.readAllBytes(p);
JsonNode type = fileNode.get("type");
String mimeType = type != null ? type.asText() : NanoHTTPD.getMimeTypeForFile(p.getFileName().toString());
return RequestBody.create(MediaType.parse(mimeType), fileBytes);
}
}
return RequestBody.create(jsonMediaType, body);
}


public static class AjaxResult {
Expand Down
Loading

0 comments on commit 18409be

Please sign in to comment.