|
1 | 1 | package com.contentstack.cms.core; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | + |
3 | 7 | import okhttp3.Interceptor; |
4 | 8 | import okhttp3.Request; |
5 | 9 | import okhttp3.Response; |
6 | | -import org.jetbrains.annotations.NotNull; |
7 | | - |
8 | | -import java.io.IOException; |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * <b>The type Header interceptor that extends Interceptor</b> |
@@ -73,16 +74,42 @@ public void setEarlyAccess(String[] earlyAccess) { |
73 | 74 | @Override |
74 | 75 | public Response intercept(Chain chain) throws IOException { |
75 | 76 | final String xUserAgent = Util.SDK_NAME + "/v" + Util.SDK_VERSION; |
76 | | - Request.Builder request = chain.request().newBuilder().header(Util.X_USER_AGENT, xUserAgent).header(Util.USER_AGENT, Util.defaultUserAgent()).header(Util.CONTENT_TYPE, Util.CONTENT_TYPE_VALUE); |
| 77 | + Request originalRequest = chain.request(); |
| 78 | + Request.Builder request = originalRequest.newBuilder() |
| 79 | + .header(Util.X_USER_AGENT, xUserAgent) |
| 80 | + .header(Util.USER_AGENT, Util.defaultUserAgent()); |
| 81 | + |
| 82 | + // Skip Content-Type header for DELETE /releases/{release_uid} request |
| 83 | + // to avoid "Body cannot be empty when content-type is set to 'application/json'" error |
| 84 | + if (!isDeleteReleaseRequest(originalRequest)) { |
| 85 | + request.header(Util.CONTENT_TYPE, Util.CONTENT_TYPE_VALUE); |
| 86 | + } |
77 | 87 |
|
78 | 88 | if (this.authtoken != null) { |
79 | 89 | request.addHeader(Util.AUTHTOKEN, this.authtoken); |
80 | 90 | } |
81 | | - if (this.earlyAccess!=null && this.earlyAccess.length > 0) { |
| 91 | + |
| 92 | + if (this.earlyAccess != null && this.earlyAccess.length > 0) { |
82 | 93 | String commaSeparated = String.join(", ", earlyAccess); |
83 | 94 | request.addHeader(Util.EARLY_ACCESS_HEADER, commaSeparated); |
84 | 95 | } |
85 | 96 | return chain.proceed(request.build()); |
86 | 97 | } |
87 | 98 |
|
| 99 | + /** |
| 100 | + * Checks if the request is a DELETE request to /releases/{release_uid} endpoint. |
| 101 | + * This endpoint should not have Content-Type header as it doesn't accept a body. |
| 102 | + * |
| 103 | + * @param request The HTTP request to check |
| 104 | + * @return true if this is a DELETE /releases/{release_uid} request |
| 105 | + */ |
| 106 | + private boolean isDeleteReleaseRequest(Request request) { |
| 107 | + if (!"DELETE".equals(request.method())) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + String path = request.url().encodedPath(); |
| 111 | + // Match pattern: /v3/releases/{release_uid} (no trailing path segments) |
| 112 | + return path.matches(".*/releases/[^/]+$"); |
| 113 | + } |
| 114 | + |
88 | 115 | } |
0 commit comments