Skip to content

Commit 59a7fe9

Browse files
author
Emre Çalışkan
committed
added support all content type and all request method
1 parent 2d0dbff commit 59a7fe9

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

README.md

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Proxy API
22

3-
Proxy API for handling POST form data requests.
3+
Proxy API for handling requests.
44

55
## Overview
66

7-
This project provides a simple proxy API for POST form data requests. It's built with Go and includes Docker support for easy deployment.
7+
This project provides a simple proxy API for requests. It's built with Go and includes Docker support for easy deployment.
88

99
## Features
1010

11-
- Proxy POST form data requests.
11+
- Proxy requests.
1212
- Built with Go.
1313
- Docker support.
1414

@@ -36,14 +36,36 @@ Configuration options can be set via environment variables:
3636

3737
- PROXY_API_PORT: The port on which the server will run (default: 9900, docker: 80).
3838

39-
## Example
39+
## Examples
4040
```sh
41-
curl --location 'localhost:9900' \
42-
--header 'proxy-url: https://httpbin.org/post' \
41+
curl --location 'localhost:9900' \
42+
--header 'X-Proxy-Url: https://httpbin.org/get'
43+
```
44+
```sh
45+
curl --location 'localhost:9900' --request POST \
46+
--header 'X-Proxy-Url: https://httpbin.org/post' \
47+
--header 'Content-Type: application/json' \
48+
--data '{
49+
"john": "doe",
50+
"foo": "bar"
51+
}'
52+
```
53+
```sh
54+
curl --location 'localhost:9900' --request POST \
55+
--header 'X-Proxy-Url: https://httpbin.org/post' \
4356
--header 'Content-Type: application/x-www-form-urlencoded' \
4457
--data-urlencode 'john=doe' \
4558
--data-urlencode 'foo=bar'
4659
```
60+
```sh
61+
curl --location 'localhost:9900' --request PUT \
62+
--header 'X-Proxy-Url: https://httpbin.org/put' \
63+
--header 'Content-Type: application/json' \
64+
--data '{
65+
"john": "doe",
66+
"foo": "bar"
67+
}'
68+
```
4769
## Development
4870

4971
1. Clone the repository:

proxy.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,33 @@ func main() {
1111
port := getPort()
1212
fmt.Printf("# Proxy API\n")
1313
fmt.Printf("# Server listening on port %s...\n", port)
14-
fasthttp.ListenAndServe(":"+port, receiveHandler)
14+
fasthttp.ListenAndServe(":"+port, requestHandler)
1515
}
1616

17-
func receiveHandler(ctx *fasthttp.RequestCtx) {
18-
if string(ctx.Method()) != fasthttp.MethodPost {
19-
sendErrorResponse(ctx, fasthttp.StatusMethodNotAllowed, "Only POST requests are allowed")
20-
return
21-
}
22-
23-
targetURL := string(ctx.Request.Header.Peek("proxy-url"))
17+
func requestHandler(ctx *fasthttp.RequestCtx) {
18+
targetURL := string(ctx.Request.Header.Peek("X-Proxy-Url"))
2419
if targetURL == "" {
25-
sendErrorResponse(ctx, fasthttp.StatusBadRequest, "Missing proxy-url header")
20+
sendErrorResponse(ctx, fasthttp.StatusBadRequest, "Missing X-Proxy-Url header")
2621
return
2722
}
2823

29-
statusCode, body, err := fasthttp.Post(nil, targetURL, ctx.PostArgs())
30-
if err != nil {
31-
sendErrorResponse(ctx, fasthttp.StatusInternalServerError, "Failed to send: "+err.Error())
24+
req := fasthttp.AcquireRequest()
25+
resp := fasthttp.AcquireResponse()
26+
defer fasthttp.ReleaseRequest(req)
27+
defer fasthttp.ReleaseResponse(resp)
28+
29+
ctx.Request.Header.CopyTo(&req.Header)
30+
req.SetBody(ctx.PostBody())
31+
req.SetRequestURI(targetURL)
32+
req.Header.SetUserAgent("Proxy API")
33+
34+
if err := fasthttp.Do(req, resp); err != nil {
35+
sendErrorResponse(ctx, fasthttp.StatusInternalServerError, err.Error())
3236
return
3337
}
3438

35-
ctx.SetStatusCode(statusCode)
36-
ctx.SetBody(body)
39+
resp.Header.CopyTo(&ctx.Response.Header)
40+
ctx.SetBody(resp.Body())
3741
}
3842

3943
func sendErrorResponse(ctx *fasthttp.RequestCtx, statusCode int, message string) {

0 commit comments

Comments
 (0)