illustrate
Here are four commonly used http request methods: GET, POST, PUT, and DELETE.
The description of the InvokeHTTP processor in the official introduction document says this:
An HTTP client processor which can interact with a configurable HTTP Endpoint. The destination URL and HTTP Method are configurable. FlowFile attributes are converted to HTTP headers and the FlowFile contents are included as the body of the request (if the HTTP Method is PUT, POST or PATCH).
Roughly speaking: This is an HTTP client handler that can interact with HTTP endpoints. The URL and HTTP Method in the handler are configurable. The attributes in the processor will be converted into HTTP request headers. If the request method is PUT, POST or PATCH, the content of the processor will be included as the body of the request (request body).
test interface
Here are several interfaces used for processor testing, which mainly correspond to GET, POST, PUT, and DELETE requests. The test code does not involve business logic and is simply used to cooperate with the test processor.
package com.example.common.resp;
import com.example.common.enums.RespEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Result {
private Integer code;
private String message;
private Object data;
// 省略部分代码。。。
public static Result ok(Object data) {
return ok(RespEnum.SUCCESS.getCode(), RespEnum.SUCCESS.getMessage(), data);
}
}
=====================================================================================
测试接口
=====================================================================================
package com.example.redis.controller;
import com.alibaba.fastjson2.JSONObject;
import com.example.common.resp.Result;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/{id}")
public Result getById(@PathVariable String id, @RequestParam Map<String, Object> query) {
JSONObject res = new JSONObject();
res.put("method", "GET");
res.put("desc", "这个是get方法");
res.put("id", id);
res.put("query", query);
return Result.ok(res);
}
@PostMapping("")
public Result add(@RequestBody JSONObject body) {
JSONObject res = new JSONObject();
res.put("method", "POST");
res.put("desc", "这个是post方法");
res.put("body", body);
return Result.ok(res);
}
@PutMapping("/{id}")
public Result editById(@PathVariable String id, @RequestBody JSONObject body) {
JSONObject res = new JSONObject();
res.put("method", "PUT");
res.put("desc", "这个是put方法");
res.put("id", id);
res.put("body", body);
return Result.ok(res);
}
@DeleteMapping("/{id}")
public Result deleteById(@PathVariable String id) {
JSONObject res = new JSONObject();
res.put("method", "DELETE");
res.put("desc", "这个是delete方法");
res.put("id", id);
return Result.ok(res);
}
}
Send GET request
Add handler:InvokeHTTP
Click Processor in the upper left corner of the toolbar and drag it to the canvas, filter out the InvokeHTTP processor and add it to the canvas
Configure processor
Double-click the added processor, switch to SETTINGS , and give the processor a name
After starting the name, switch to PROPERTIES , configure the following content, and finally click APPLY
Add custom request headers
If you need to add custom attributes to the request header, just click the plus sign + add in the upper right corner. For example, if I add a myToken request header here, the request header sent at this time will carry this parameter.
test processor
To facilitate testing, set the InvokeHTTP processor to execute once every 10 seconds.
Similarly, add the LogAttribute processor to the canvas
Modify the configuration of the LogAttribute processor, and change the property value of Log Payload in the PROPERTIES tab to true
Connect the two processors, select Response for the relationship, and finally click ADD
Select terminate for all RELATIONSHIPS of the first InvokeHTTP processor except Response.
Select the RELATIONSHIPS of the second LogAttribute processor itself to terminate
If all the processors are displayed as shown in the picture, it means there is no problem and it can run.
Start two processors, right-click on a blank position of the canvas and select Start
In nifi's log, you can see that the results returned by the interface are printed every ten seconds.
Send POST request
To send a POST request carrying request body data, we need to use another processor: GenerateFlowFile
Add processor: GenerateFlowFile
Configure processor
Double-click the added processor and give the processor a name in the SETTINGS tab
Switch to the PROPERTIES tab, and then configure the values of the Custom Text and Mime Type properties respectively. The values of other properties can be defaulted.
Custom Text: (Official explanation) If Data Format is text and if Unique FlowFiles is false, then this custom text will be used as content of the generated FlowFiles and the File Size will be ignored. In layman's terms, if the value in Custom Text is of text type and the value of the Unique FlowFiles attribute is false, the value in Custom Text will be regarded as the content of the flow file, so that the value in Custom Text is passed to the next processor, InvokeHTTP. Then it will be passed to the interface as the request body data.
Add handler:InvokeHTTP
Just like sending a GET request, add the InvokeHTTP processor, and then configure the following content. The request body in the post request comes from the content of the previous processor
test processor
Connect the first processor, GenerateFlowFile, to the second processor, InvokeHTTP, and select success for the association relationship
Continue to connect the second processor InvokeHTTP with the previously added LogAttribute processor, select Response for the association relationship
Similarly, select terminate for all RELATIONSHIPS of the InvokeHTTP processor except Response
For the same convenient test, set the first processor GenerateFlowFile to be executed every 10 seconds, and then start the three processors corresponding to the POST method respectively (mouse over the corresponding processor and right-click to select Start)
View the log of nifi, you can see that the result returned by the interface will be printed every ten seconds
Send PUT request
The put request is similar to the post request. The processor can be added again according to the post request. You only need to change the request method in the InvokeHTTP processor to PUT and the URL to the corresponding request path. I won’t add it again here. The following is after adding it.
Test Results
Send DELETE request
The put request is similar to the get request. The processor can be added again according to the get request. You only need to change the request method in the InvokeHTTP processor to DELETE and the URL to the corresponding request path. I won’t repeat the addition here, the following is after adding
Test Results
Conclusion
The above is about the use of InvokeHTTP in NIFI. If you have any questions, please comment.