I try to call an grpc python service with NestJS client I pass my parameter on the call and the python API return to me an error “Invalid URL: The ‘pdf_url’ field is empty”.
When I try to do the call with my terminal the parameter is correctly receive by my python API.
So I don’t know where my NestJS conf is not good.
pdf.proto (NestJS & Python service)
syntax = "proto3";
package pdfwhitespace;
service PDFWhitespaceService {
rpc FindWhitestSpace (PDFRequest) returns (WhitespaceResponse) {}
}
message PDFRequest {
string pdf_url = 1;
}
message WhitespaceResponse {
message PageWhitespace {
int32 page_number = 1;
float left = 2;
float top = 3;
float width = 4;
float height = 5;
}
repeated PageWhitespace whitespaces = 1;
}
server.py
class PDFWhitespaceServicer(pdf_whitespace_pb2_grpc.PDFWhitespaceServiceServicer):
def FindWhitestSpace(self, request, context):
print(f"Raw request received: {request}")
if not request.pdf_url:
print("Field 'pdf_url' is missing or empty")
context.set_details("Invalid URL: The 'pdf_url' field is empty")
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
return pdf_whitespace_pb2.WhitespaceResponse()
print(f"Received URL: {request.pdf_url}")
NestJS config :
app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
load: [
...
PdfWhitespaceMicroserviceConfig,
],
}),
...
],
controllers: [...],
providers: [...],
})
microservice.config.js
import { registerAs } from '@nestjs/config';
import { Transport } from '@nestjs/microservices';
import { join } from 'path';
export default registerAs('pdf-whitespace-microservice', () => ({
transport: Transport.GRPC,
options: {
url: 'pdf-whitespace-microservice:50051',
package: 'pdfwhitespace', // Doit correspondre au package défini dans .proto
protoPath: join(__dirname, '../../protos/pdf_whitespace.proto'), // Chemin correct
},
}));
pdf.service.ts
interface PageWhitespace {
page_number: number;
left: number;
top: number;
width: number;
height: number;
}
interface WhitespaceResponse {
whitespaces: PageWhitespace[];
}
interface GRPCPdfService {
FindWhitestSpace(data: string): Observable<WhitespaceResponse>;
}
@Injectable()
export class PdfService extends ALoggerService {
private grpcService: GRPCPdfService;
constructor(@Inject('PDF_WHITESPACE_PACKAGE') private client: ClientGrpc) {
super();
try {
this.grpcService = this.client.getService<GRPCPdfService>(
'PDFWhitespaceService',
);
this.logger.debug('PDF service connection initialized successfully');
} catch (error) {
this.logger.error('Failed to initialize PDF service', error);
throw error;
}
}
async findWhitestSpace(pdf_url: string): Promise<PageWhitespace[]> {
if (!pdf_url) {
throw new Error('pdf_url cannot be empty');
}
console.log(`'{'pdf_url': '${pdf_url}'}'`);
// const req = this.grpcService.FindWhitestSpace();
return new Promise((resolve, reject) => {
this.grpcService.FindWhitestSpace(`{pdf_url:plop}`).subscribe(
(response) => {
console.log('Response received:', response);
resolve(response.whitespaces);
},
(error) => {
console.error('gRPC error:', error);
reject(error);
},
);
});
}
}
On my terminal I try with this command line :
grpcurl -plaintext -d '{"pdf_url": "https://example.com"}' localhost:50051 pdfwhitespace.PDFWhitespaceService/FindWhitestSpace
And it’s return “Received URL: https://example.com”
What’s wrong with my NestJS config ?